2014-09-11 14:25:43 +00:00
|
|
|
|
---
|
|
|
|
|
title: Angular.js
|
2015-11-24 05:09:17 +00:00
|
|
|
|
category: JavaScript libraries
|
2023-03-14 01:52:21 +00:00
|
|
|
|
tags: [Archived]
|
|
|
|
|
archived: This describes an older version of Angular.
|
2014-09-11 14:25:43 +00:00
|
|
|
|
---
|
2023-03-14 01:52:21 +00:00
|
|
|
|
|
|
|
|
|
### About
|
|
|
|
|
{: .-intro}
|
|
|
|
|
|
|
|
|
|
* <https://github.com/angular/angular-seed>
|
|
|
|
|
* <https://angularjs.org/>
|
|
|
|
|
|
|
|
|
|
### ng-app
|
|
|
|
|
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<html ng-app="nameApp">
|
|
|
|
|
```
|
2014-09-11 14:25:43 +00:00
|
|
|
|
|
|
|
|
|
### Lists (ng-repeat)
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```html
|
2014-09-11 14:25:43 +00:00
|
|
|
|
<ul ng-controller="MyListCtrl">
|
|
|
|
|
<li ng-repeat="phone in phones">
|
|
|
|
|
{{phone.name}}
|
|
|
|
|
</li>
|
2018-02-12 23:06:00 +00:00
|
|
|
|
</ul>
|
|
|
|
|
```
|
2014-09-11 14:25:43 +00:00
|
|
|
|
|
|
|
|
|
### Model (ng-model)
|
|
|
|
|
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```html
|
2014-09-11 14:25:43 +00:00
|
|
|
|
<select ng-model="orderProp">
|
|
|
|
|
<option value="name">Alphabetical</option>
|
|
|
|
|
<option value="age">Newest</option>
|
2018-02-12 23:06:00 +00:00
|
|
|
|
</select>
|
|
|
|
|
```
|
2014-09-11 14:25:43 +00:00
|
|
|
|
|
|
|
|
|
### Defining a module
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```js
|
|
|
|
|
App = angular.module('myApp', []);
|
2014-09-11 14:25:43 +00:00
|
|
|
|
|
|
|
|
|
App.controller('MyListCtrl', function ($scope) {
|
|
|
|
|
$scope.phones = [ ... ];
|
2018-02-12 23:06:00 +00:00
|
|
|
|
});
|
|
|
|
|
```
|
2014-09-11 14:25:43 +00:00
|
|
|
|
|
|
|
|
|
### Controller with protection from minification
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```js
|
|
|
|
|
App.controller('Name', [
|
2014-09-11 14:25:43 +00:00
|
|
|
|
'$scope',
|
|
|
|
|
'$http',
|
|
|
|
|
function ($scope, $http) {
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
a.c 'name', [
|
|
|
|
|
'$scope'
|
|
|
|
|
'$http'
|
|
|
|
|
($scope, $http) ->
|
2018-02-12 23:06:00 +00:00
|
|
|
|
]
|
|
|
|
|
```
|
2014-09-11 14:25:43 +00:00
|
|
|
|
|
2015-05-11 17:31:20 +00:00
|
|
|
|
### Service
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```js
|
|
|
|
|
App.service('NameService', function($http){
|
2015-05-11 17:31:20 +00:00
|
|
|
|
return {
|
|
|
|
|
get: function(){
|
|
|
|
|
return $http.get(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-12 23:06:00 +00:00
|
|
|
|
});
|
|
|
|
|
```
|
2015-05-11 17:31:20 +00:00
|
|
|
|
In controller you call with parameter and will use promises to return data from server.
|
|
|
|
|
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```js
|
|
|
|
|
App.controller('controllerName',
|
2015-05-11 17:31:20 +00:00
|
|
|
|
function(NameService){
|
|
|
|
|
NameService.get()
|
|
|
|
|
.then(function(){})
|
2018-02-12 23:06:00 +00:00
|
|
|
|
})
|
|
|
|
|
```
|
2015-05-11 17:31:20 +00:00
|
|
|
|
|
|
|
|
|
### Directive
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```js
|
|
|
|
|
App.directive('name', function(){
|
2015-05-11 17:31:20 +00:00
|
|
|
|
return {
|
2015-08-13 14:12:36 +00:00
|
|
|
|
template: '<h1>Hello</h1>'
|
2015-05-11 17:31:20 +00:00
|
|
|
|
}
|
2018-02-12 23:06:00 +00:00
|
|
|
|
});
|
|
|
|
|
```
|
2015-05-11 17:31:20 +00:00
|
|
|
|
|
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
|
2018-02-12 23:06:00 +00:00
|
|
|
|
```js
|
|
|
|
|
App.controller('PhoneListCtrl', function ($scope, $http) {
|
|
|
|
|
$http.get('/data.json').success(function (data) {
|
|
|
|
|
$scope.phones = data;
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
```
|