Cheat Sheet di AngularJS
AngularJS è un framework JavaScript.
Elenco (ng-repeat)
<ul ng-controller="MyListCtrl">
<li ng-repeat="phone in phones">
{{phone.name}}
</li>
</ul>Modello (ng-model)
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>Definire un modulo
App = angular.module('myApp', []);
App.controller('MyListCtrl', function ($scope) {
$scope.phones = [ ... ];
});Prevenire la minificazione del controller
App.controller('Name', [
'$scope',
'$http',
function ($scope, $http) {
}
]);
a.c 'name', [
'$scope'
'$http'
($scope, $http) ->
]Servizi
App.service('NameService', function($http){
return {
get: function(){
return $http.get(url);
}
}
});La chiamata con parametri nel controller utilizzerà le promise per restituire i dati dal server.
App.controller('controllerName',
function(NameService){
NameService.get()
.then(function(){})
})Direttive
App.directive('name', function(){
return {
template: '<h1>Hello</h1>'
}
});In HTML, l’uso di <name></name> renderizzerà il template <h1>Hello</h1>
HTTP
App.controller('PhoneListCtrl', function ($scope, $http) {
$http.get('/data.json').success(function (data) {
$scope.phones = data;
})
});Riferimenti: