@duanyubin
2015-02-12T02:16:59.000000Z
字数 4203
阅读 480
<input ng-model="hehe" >$scope.hehe = 'haha'{{ expression | filter1 | filter2 | ... }}$scope $http

angular.module('myApp').directive('myDirective', function($timeout, UserDefinedService) {// directive definition goes here}).directive('mySecondDirective', ['$timeout', 'UserDefinedService', function($timeout, UserDefinedService){}])
$rootScope$on, $emit, $broadcast
myApp.controller('GreetingController', ['$scope', function($scope) {$scope.greeting = 1;$scope.addOne = function(){$scope.greeting++;};}]);myApp.controller('DoubleController', ['$scope',function($scope) {$scope.double = function(value) { return value * 2; };}]);
<div ng-controller="GreetingController"><button ng-click="addOne()">Add one</button>{{ greeting }}</div><div ng-controller="DoubleController">Two times <input ng-model="num"> equals {{ double(num) }}</div>
// Scope 作用域myApp.controller('MainController', ['$scope', function($scope) {$scope.timeOfDay = 'morning';$scope.name = 'Nikki';}]);myApp.controller('ChildController', ['$scope', function($scope) {$scope.name = 'Mattie';}]);myApp.controller('GrandChildController', ['$scope', function($scope) {$scope.timeOfDay = 'evening';$scope.name = 'Gingerbread Baby';$scope.parent = $scope.$parent.$parent.name;}]);
<div class="spicy"><div ng-controller="MainController"><p>Good {{timeOfDay}}, {{name}}!</p><div ng-controller="ChildController"><p>Good {{timeOfDay}}, {{name}}!</p><div ng-controller="GrandChildController"><p>Good {{timeOfDay}}, {{name}}!</p><p>{{parent}}</p></div></div></div></div>
ngRoute
angular.module('myApp', []).directive('myDirective', function() {return {restrict: String,priority: Number,terminal: Boolean,template: String or Template Function: function(tElement, tAttrs) (...},templateUrl: String,replace: Boolean or String,scope: Boolean or Object,transclude: Boolean,controller: String or function(scope, element, attrs, transclude, otherInjectables) { ... },controllerAs: String,require: String,link: function(scope, iElement, iAttrs) { ... },compile: return an Object OR function(tElement, tAttrs, transclude) {return {pre: function(scope, iElement, iAttrs, controller) { ... },post: function(scope, iElement, iAttrs, controller) { ... }}// orreturn function postLink(...) { ... }}};});
• E (an element)
• A (an attribute, default) <div my-directive=”expression”></div>
• C (a class) <div class=”my-directive: expression;”></div>
• M (a comment) <– directive: my-directive expression –>
<div sidebox title="Links"><ul><li>First link</li><li>Second link</li></ul></div>
angular.module('myApp', []).directive('sidebox', function() {return {restrict: 'EA',scope: {title: '@'},transclude: true,template: '<div class="sidebox">\<div class="content">\<h2 class="header">{{ title }}</h2>\<span class="content" ng-transclude></span>\</div>\</div>'}});
Compile
compile: function(tEle, tAttrs, transcludeFn) {var tplEl = angular.element('<div>' +'<h2></h2>' +'</div>');var h2 = tplEl.find('h2');h2.attr('type', tAttrs.type);h2.attr('ng-model', tAttrs.ngModel);h2.val("hello");tEle.replaceWith(tplEl);return function(scope, ele, attrs) {// The link function}}
Link
link: function (scope, element, attrs) {// manipulate the DOM in here}
arguments: ($scope, $element, $attrs, $transclude)
The link function provides isolation between directives, while the controller defines shareable behavior.