[关闭]
@xiaoqq 2016-08-03T12:13:41.000000Z 字数 5098 阅读 1059

AngularJS学习笔记——指令

AngularJS


本文摘自《AngularJS权威教程》

一、 介绍:一种构建动态Web应用的结构化框架。

二、 指令:本质上就是AngularJS扩展具有自定义功能的HTML元素的途径。

  1. <video my-href="/goofy-video.mp">
  2. Can still take children nodes
  3. </video>
  1. angular.module('myApp', [])
  2. .directive('myDirective', function() {
  3. return {
  4. restrict: 'E',
  5. template: '<a href="http://google.com">,Click me to go to Google < /a>'
  6. };
  7. });

三、 内置指令:以ng为前缀的标签

除了<form><a>标签被重载以外,通常以ng为前缀。<form>标签被从底层扩展了一系列高级功能,包括表单验证等

(1) 基础ng指令_布尔属性:布尔属性代表一个true或false值。当这个属性出现时,这个属性的值就是true(无论实际定义的值是什么)。如果未出现,这个属性的值就是false。

  1. <textarea ng-disabled="isDisabled">Wait5seconds</textarea>
  2. angular.module('myApp', [])
  3. .run(function($rootScope, $timeout) {
  4. $rootScope.isDisabled = true;
  5. $timeout(function() {
  6. $rootScope.isDisabled = false;
  7. }, 5000);
  8. });

注意:为什么这里使用$timeout而不直接使用setTimeout。因为$apply都会自动执行,所以你不用担心,但是如果你使用了angular之外的功能,比如直接调用了setTimeout函数、挂接了jquery的事件、使用了jquery的ajax操作等等,那么系统就没有机会帮你调用$apply,界面也就没有机会刷新了

  1. <select>
  2. <option>One Fish</option>
  3. <option ng-selected="isTwoFish">Two Fish</option>
  4. </select>

(2) 基础ng指令_类布尔属性:ng-hrefng-src等属性虽然不是标准的HTML布尔属性,但是由于行为相似,所以在AngularJS源码内部是和布尔属性同等对待的。

(3) 创建子作用域的ng指令:下列指令在使用中会自动创建子域: ng-controller,ng-include,ng-switch,ng-repeat,ng-view,ng-if

  1. <div ng-controller="ParentController">
  2. {{someBareValue}}
  3. <button ng-click="someAction()">Communicate to child</button>
  4. <div ng-controller="ChildController">
  5. {{someBareValue}}
  6. <button ng-click="childAction()">to parent</button>
  7. </div>
  8. </div>
  9. <script type="text/javascript">
  10. angular.module('myApp', [])
  11. .controller('ParentController', ['$scope', function($scope) {
  12. $scope.someBareValue = "parent";
  13. $scope.model = {
  14. name : "pengchen"
  15. };
  16. $scope.someAction = function() {
  17. $scope.someBareValue = "from parent";
  18. }
  19. }]).controller('ChildController', function($scope) {
  20. $scope.someBareValue = "child";
  21. $scope.$parent.someBareValue = "change";
  22. $scope.model.name = "tangmin";
  23. console.log($scope.model.name);
  24. console.log($scope.$parent.model.name);
  25. $scope.childAction = function() {
  26. $scope.someBareValue = "from child";
  27. }
  28. });
  29. </script>

代码中,ChildController中的$scope可以访问并修改ParentController$scope的属性,反之则不行。

  1. <input type="text" ng-model="person.name"/>
  2. <div ng-switch on="person.name">
  3. <p ng-switch-default>And the winner is</p>
  4. <h1 ng-switch-when="Ari">{{ person.name }}</h1>
  5. </div>
  1. <li ng-repeat="person in people" ng-class="{even: !$even, odd: !$odd}">
  2. {{person.name}} lives in {{person.city}}
  3. </li>

(4) 其他指令:

四、 指令详解:以ng为前缀的标签

(1). 指令定义:使用directive()进行定义

以下是配置指令的全部选项设置:

  1. angular.module('myApp', []).directive('myDirective', function() {
  2. return {
  3. restrict: String,
  4. priority: Number,
  5. terminal: Boolean,
  6. template: String or Template Function: function(tElement, tAttrs)(...
  7. },
  8. templateUrl: String,
  9. replace: Boolean or String,
  10. scope: Boolean or Object,
  11. transclude: Boolean,
  12. controller: String or
  13. function(scope, element, attrs, transclude, otherInjectables) {...
  14. },
  15. controllerAs: String,
  16. require: String,
  17. link: function(scope, iElement, iAttrs) {...
  18. },
  19. compile: // 返回一个对象或连接函数,如下所示:
  20. function(tElement, tAttrs, transclude) {
  21. return {
  22. pre: function(scope, iElement, iAttrs, controller) {...
  23. },
  24. post: function(scope, iElement, iAttrs, controller) {...
  25. }
  26. }
  27. // 或者
  28. return function postLink(...) {...
  29. }
  30. }
  31. };
  32. });

(2). 指令作用域:

(3). 绑定策略:

使新的指令作用域访问本地变量:

a) 本地作用域属性:使用@符号将本地作用域同DOM属性的值进行绑定
b) 双向绑定:通过=可以将本地作用域上的属性同父级作用域上的属性进行双向的数据绑定;就像普通的数据绑定一样,本地属性会反映出父数据模型中所发生的改变
c) 父级作用域绑定 通过&符号可以对父级作用域进行绑定,以便在其中运行函数; 意味着对这个值进行设置时会生成一个指向父级作用域的包装函数

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