Will Fuqua | Bangkok .NET Users Group | April 2014
Hello, {{nameJsDemo}}
var myInput = document.getElementById("my-input-box");
var myLabel = document.getElementById("my-label");
myInput.addEventListener("keyup", function() {
myLabel.innerText = "Hello, " + this.value;
}, false);
Hello, {{name}}
Hello, {{name}}
<!DOCTYPE html>
<html ng-app="favorites-app">
<head>
<script src="angular.min.js"></script>
<script>
var app = angular.module('favorites-app', []);
app.controller('FavoritesController', function($scope) {
$scope.favoriteThings = ['JavaScript', '.NET', 'Unicorns'];
});
</script>
</head>
<body ng-controller="FavoritesController">
<h1>My Favorite Things</h1>
<ul>
<li ng-repeat="favorite in favoriteThings">{{favorite}}</li>
</ul>
</body>
</html>
app.controller('FavoritesController', function($scope) {
$scope.favoriteThings = ['JavaScript', '.NET', 'Unicorns'];
});
Uses Dependency Injection
app.controller('FavoritesController', function($log) {
$log.debug('now we have logging!');
});
app.controller('FavoritesController', function($http) {
// make an http request!
$http.get('/api/favorite-things').success(function(data) {
...
});
});
app.controller('FavoritesController', function($scope, $http, $log) {
$log.debug('Making http request...');
$http.get('/api/favorite-things').success(function(favorites) {
$log.debug('...received response');
$scope.favoriteThings = favorites;
});
});
$scope
, holds data for the view:
...
{
$scope.favoriteThings = ["C#", "AngularJS"];
$scope.mostFavoriteThing = "Caffeine";
}
...
{
$scope.favoriteThings = ["C#", "AngularJS"];
$scope.addFavorite = function(newFavorite) {
$scope.favoriteThings.push(newFavorite);
}
}
<ul>
<li ng-repeat="favorite in favoriteThings">{{favorite}}</li>
</ul>
<input type="text" ng-model="newFavorite" />
<button ng-click="addFavorite(newFavorite)">Add</button>
image sources