AngularJS 치트 시트
AngularJS는 JavaScript 프레임워크입니다.
목록 (ng-repeat)
<ul ng-controller="MyListCtrl">
<li ng-repeat="phone in phones">
{{phone.name}}
</li>
</ul>모델 (ng-model)
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>모듈 정의
App = angular.module('myApp', []);
App.controller('MyListCtrl', function ($scope) {
$scope.phones = [ ... ];
});컨트롤러 압축(Minification) 방지
App.controller('Name', [
'$scope',
'$http',
function ($scope, $http) {
}
]);
a.c 'name', [
'$scope'
'$http'
($scope, $http) ->
]서비스 (Services)
App.service('NameService', function($http){
return {
get: function(){
return $http.get(url);
}
}
});컨트롤러에서 매개변수를 사용하여 호출하면 promise를 사용하여 서버로부터 데이터를 반환합니다.
App.controller('controllerName',
function(NameService){
NameService.get()
.then(function(){})
})디렉티브 (Directives)
App.directive('name', function(){
return {
template: '<h1>Hello</h1>'
}
});HTML에서 <name></name>을 사용하면 템플릿인 <h1>Hello</h1>이 렌더링됩니다.
HTTP
App.controller('PhoneListCtrl', function ($scope, $http) {
$http.get('/data.json').success(function (data) {
$scope.phones = data;
})
});참고: