cheatsheets/angularjs.md

95 lines
1.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Angular.js
category: JavaScript libraries
---
```html
   <html ng-app="nameApp">
```
### Lists (ng-repeat)
```html
<ul ng-controller="MyListCtrl">
<li ng-repeat="phone in phones">
{{phone.name}}
</li>
   </ul>
```
### Model (ng-model)
```html
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
   </select>
```
### Defining a module
```js
   App = angular.module('myApp', []);
App.controller('MyListCtrl', function ($scope) {
$scope.phones = [ ... ];
   });
```
### Controller with protection from minification
```js
   App.controller('Name', [
'$scope',
'$http',
function ($scope, $http) {
}
]);
a.c 'name', [
'$scope'
'$http'
($scope, $http) ->
   ]
```
### Service
```js
   App.service('NameService', function($http){
return {
get: function(){
return $http.get(url);
}
}
   });
```
In controller you call with parameter and will use promises to return data from server.
```js
   App.controller('controllerName',
function(NameService){
NameService.get()
.then(function(){})
   })
```
### Directive
```js
   App.directive('name', function(){
return {
template: '<h1>Hello</h1>'
}
   });
```
In HTML will use `<name></name>` to render your template `<h1>Hello</h1>`
### HTTP
```js
App.controller('PhoneListCtrl', function ($scope, $http) {
$http.get('/data.json').success(function (data) {
$scope.phones = data;
})
   });
```
References:
* https://github.com/angular/angular-seed
* https://angularjs.org/