[关闭]
@duanyubin 2015-02-12T02:16:59.000000Z 字数 4203 阅读 337

Angular in action

Contents

demo

Basic Concept

Data-binding

Dependency Injection

  1. angular.module('myApp')
  2. .directive('myDirective', function($timeout, UserDefinedService) {
  3. // directive definition goes here
  4. })
  5. .directive('mySecondDirective', ['$timeout', 'UserDefinedService', function($timeout, UserDefinedService){
  6. }])

Controller

$scope

  1. myApp.controller('GreetingController', ['$scope', function($scope) {
  2. $scope.greeting = 1;
  3. $scope.addOne = function(){
  4. $scope.greeting++;
  5. };
  6. }]);
  7. myApp.controller('DoubleController', ['$scope',function($scope) {
  8. $scope.double = function(value) { return value * 2; };
  9. }]);
  1. <div ng-controller="GreetingController">
  2. <button ng-click="addOne()">Add one</button>
  3. {{ greeting }}
  4. </div>
  5. <div ng-controller="DoubleController">
  6. Two times <input ng-model="num"> equals {{ double(num) }}
  7. </div>

Hierarchy

  1. // Scope 作用域
  2. myApp.controller('MainController', ['$scope', function($scope) {
  3. $scope.timeOfDay = 'morning';
  4. $scope.name = 'Nikki';
  5. }]);
  6. myApp.controller('ChildController', ['$scope', function($scope) {
  7. $scope.name = 'Mattie';
  8. }]);
  9. myApp.controller('GrandChildController', ['$scope', function($scope) {
  10. $scope.timeOfDay = 'evening';
  11. $scope.name = 'Gingerbread Baby';
  12. $scope.parent = $scope.$parent.$parent.name;
  13. }]);
  1. <div class="spicy">
  2. <div ng-controller="MainController">
  3. <p>Good {{timeOfDay}}, {{name}}!</p>
  4. <div ng-controller="ChildController">
  5. <p>Good {{timeOfDay}}, {{name}}!</p>
  6. <div ng-controller="GrandChildController">
  7. <p>Good {{timeOfDay}}, {{name}}!</p>
  8. <p>{{parent}}</p>
  9. </div>
  10. </div>
  11. </div>
  12. </div>

Service

Router ngRoute

demo

Directive

Basic

Passing Data into a Directive

demo

Built-In Directives

Explained

  1. angular.module('myApp', [])
  2. .directive('myDirective', function() {
  3. return {
  4. restrict: String,
  5. priority: Number,
  6. terminal: Boolean,
  7. template: String or Template Function: function(tElement, tAttrs) (...},
  8. templateUrl: String,
  9. replace: Boolean or String,
  10. scope: Boolean or Object,
  11. transclude: Boolean,
  12. controller: String or function(scope, element, attrs, transclude, otherInjectables) { ... },
  13. controllerAs: String,
  14. require: String,
  15. link: function(scope, iElement, iAttrs) { ... },
  16. compile: return an Object OR function(tElement, tAttrs, transclude) {
  17. return {
  18. pre: function(scope, iElement, iAttrs, controller) { ... },
  19. post: function(scope, iElement, iAttrs, controller) { ... }
  20. }
  21. // or
  22. return function postLink(...) { ... }
  23. }
  24. };
  25. });

restrict

• 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 –>

Scope

Transclude

  1. <div sidebox title="Links">
  2. <ul>
  3. <li>First link</li>
  4. <li>Second link</li>
  5. </ul>
  6. </div>
  1. angular.module('myApp', [])
  2. .directive('sidebox', function() {
  3. return {
  4. restrict: 'EA',
  5. scope: {
  6. title: '@'
  7. },
  8. transclude: true,
  9. template: '<div class="sidebox">\
  10. <div class="content">\
  11. <h2 class="header">{{ title }}</h2>\
  12. <span class="content" ng-transclude></span>\
  13. </div>\
  14. </div>'
  15. }
  16. });

Require (string|array)

Compile

  1. compile: function(tEle, tAttrs, transcludeFn) {
  2. var tplEl = angular.element('<div>' +
  3. '<h2></h2>' +
  4. '</div>');
  5. var h2 = tplEl.find('h2');
  6. h2.attr('type', tAttrs.type);
  7. h2.attr('ng-model', tAttrs.ngModel);
  8. h2.val("hello");
  9. tEle.replaceWith(tplEl);
  10. return function(scope, ele, attrs) {
  11. // The link function
  12. }
  13. }

Link

  1. link: function (scope, element, attrs) {
  2. // manipulate the DOM in here
  3. }

Controller (String or Function)

arguments: ($scope, $element, $attrs, $transclude)

The link function provides isolation between directives, while the controller defines shareable behavior.

实际案例

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注