cheatsheets/angularjs.md

102 lines
1.8 KiB
Markdown
Raw Permalink Normal View History

2014-09-11 14:25:43 +00:00
---
title: Angular.js
2015-11-24 05:09:17 +00:00
category: JavaScript libraries
tags: [Archived]
archived: This describes an older version of Angular.
2014-09-11 14:25:43 +00:00
---
### About
{: .-intro}
* <https://github.com/angular/angular-seed>
* <https://angularjs.org/>
### ng-app
```html
   <html ng-app="nameApp">
```
2014-09-11 14:25:43 +00:00
### Lists (ng-repeat)
```html
2014-09-11 14:25:43 +00:00
<ul ng-controller="MyListCtrl">
<li ng-repeat="phone in phones">
{{phone.name}}
</li>
   </ul>
```
2014-09-11 14:25:43 +00:00
### Model (ng-model)
```html
2014-09-11 14:25:43 +00:00
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
   </select>
```
2014-09-11 14:25:43 +00:00
### Defining a module
```js
   App = angular.module('myApp', []);
2014-09-11 14:25:43 +00:00
App.controller('MyListCtrl', function ($scope) {
$scope.phones = [ ... ];
   });
```
2014-09-11 14:25:43 +00:00
### Controller with protection from minification
```js
   App.controller('Name', [
2014-09-11 14:25:43 +00:00
'$scope',
'$http',
function ($scope, $http) {
}
]);
a.c 'name', [
'$scope'
'$http'
($scope, $http) ->
   ]
```
2014-09-11 14:25:43 +00:00
### 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 {
2015-08-13 14:12:36 +00:00
template: '<h1>Hello</h1>'
}
   });
```
2015-05-11 17:34:08 +00:00
In HTML will use `<name></name>` to render your template `<h1>Hello</h1>`
2014-09-11 14:25:43 +00:00
### HTTP
```js
App.controller('PhoneListCtrl', function ($scope, $http) {
$http.get('/data.json').success(function (data) {
$scope.phones = data;
})
   });
```