1. 模块的利用扩充
模块的名称也可以当做变量使用,例如:
1 <body ng-app> 2 <label><input type="checkbox" ng-model="checked"/>Toggle Button</label> 3 <button ng-disabled="checked">Press me</button> 4 </body>
2. directive指令机制
指令的作用:实现语义化标签
在首页上我们可以随便定义标签使用,这一段标签里可以书写内容也可以不写。后台js可以使用方法替换这段内容,替换的可以是一大段html代码,甚至可以是一个完整的页面:
<body ng-app="MyApp"> <my> </my> </body>
1 var app = angular.module("MyApp", []); 2 3 app.directive("myWidgetFirst", function() { 4 var linkFunction_nice = function(scope, element, attributes) { 5 var paragraph = element.children()[0]; 6 //ag内部实现的一个选择器操作 7 $(paragraph).on("click", function() { 8 $(this).css({ "background-color": "red" }); 9 }); 10 }; 11 12 return { 13 restrict: "E", //暴露的一个元素,若是‘A’则是属性 14 replace: true, 15 templateUrl: 'js/template/test_index.html' 16 }; 17 });
上面一个就是替换标签<myWidgetFirst>的。
返回的内容在return当中,第一个restrict后面返回的参数有4中,
E,element代表元素 A代表属性,C代表样式css,M代表注释。
replace:true是否替换,flase就不会替换
templateUrl:替换的网页,如果想要替换其他东西,如html代码,可以这么写:
template:"<div>aaa</div>"
directive指令机制的原理是自定义一个属性或者标签来使用,因为一般来说angular自带的标签不是很够用,所以需要额外的根据需要去定义。如果定义的是标签,如
<apple></apple>
那么,这个标签返回内容return中的restirct的值便是E,如果定义的是属性,如:
<p apple="appletree" yahaha="yohoho"></p>
那么返回值里面的return中的restrict的值就是A。
总结一下,directive的return中应该有多少东西——
1 var phonecatDirectives = angular.module('phonecatDirectives', []); 2 3 phonecatDirectives.directive('directiveName', function($inject) { 4 5 return { 6 template: '<div></div>', 7 8 replace: false, 9 10 transclude: true, 11 12 restrict: 'E', 13 14 scope: { ... }, 15 16 controller: function($scope, $element){ .... }, 17 18 compile: function(tElement, tAttrs, transclude) { 19 return { 20 pre: function preLink(scope, iElement, iAttrs, controller) { ... }, 21 post: function postLink(scope, iElement, iAttrs, controller) { ... } 22 } 23 }, 24 25 link: function(scope, iElement, iAttrs) { ... } 26 }; 27 })
完整来讲,应该有这么多。但是我们并不是都要使用,选择需要的就可以。现在对所有的属性进行翻译-
template:模板,即替换内容,如果replace 为true,则将模版内容替换当前的HTML元素,并将原来元素的属性、class一并迁移;如果为false,则将模版元素当作当前元素的子元素处理。
transculde:编译转换,一般模板中会出现带有ng-transclude属性的标签,这个值设为true的时候会启动编译,将里面的代码经过编译后输出:
1 app.directive("secondWidget", function(){ 2 return { 3 restrict: "E", 4 transclude: true, 5 scope: { 6 title: '@title' 7 }, 8 template: "<div ng-transclude>{{title}}</div>" 9 }; 10 })
如这个,div中出现ng-transclude属性,后面的是{{title}}。
如果没有这个属性,那么这个secondWidget标签的内容会被模板替代,如果有这个属性,那么模板内容会将标签内容包裹起来放到里面。于是,上面的结果就是;
1 <second-widget title="aaa" class="ng-isolate-scope ng-scope"><div ng-transclude="" class="ng-binding">aaa 2 <p class="ng-scope">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere, fugit, voluptate ad voluptas ullam sunt vitae eligendi asperiores consequuntur ex sequi quisquam rem animi ut modi unde neque optio in!</p> 3 </div></second-widget>
scope:定义,这是一个对象,对象里面有着各种各样定义的东西,如:
scope: {
title: '@title'
},
template: "<div ng-transclude>{{title}}</div>"
这里的title对应着下面的模板的title,而后面的字符串"@title"代表标签上title属性值,@代表单向绑定。
只写一个@其实也可以,但是属性值的名字就必须是变量的名字,如果属性名称发生改变就不行了。例如——
title: '@'
<div titlea="aaa"></div>
这种情况下就不行了,只有
<div title="aaa"></div>
才能使用。
有@,其实也有其他的符号,单向绑定@,双向绑定的=,还有函数专用的 &
但双向绑定没什么好讲的,关于函数必须讲一下:
app.directive("myWidgetExpr", function() {
var linkFunction = function(scope, element, attributes) {
scope.text = scope.fna({ count: 5 });
};
return {
restrict: "E",
template: "<p>{{text}}</p>",
link: linkFunction,
scope: {
fna: "&fn"
}
};
<my-widget-expr fn="count = count + 1"></my-widget-expr>
这个代码中,得到的结果是6.
标签中函数可以写一些简单的表达式,如上面的
"count = count + 1"
表达式写在引号当中,这个表达式可以有多个参数,但是每个参数都需要在后台声明,所有的参数都写在一个对象中:
scope.text = scope.fna({ count: 5 ,max:5});
你可以随便定义多个其他值不用,但不能让函数被调用的时候缺乏这个参数,做到宁滥勿缺。
这个对象中也可以写函数——
scope.text = scope.fna({ count: 5 ,max:function(count){
return count*2
}});
<my-widget-expr fn="max(2) "></my-widget-expr>
调用起来意外地好用。但是不知道为什么不能使用同一个对象的属性,如:
1 app.directive("myWidgetExpr", function() { 2 var linkFunction = function(scope, element, attributes) { 3 scope.ab={ 4 a: 5 , 5 // c:a, 6 b:function(){ 7 8 9 10 alert(this.a) 11 } 12 } 13 scope.text = scope.fna(scope.ab); 14 // console.log(scope.ab.b()) 15 console.log( scope.fna(scope.ab)) 16 };
不知道为什么就是出错
3. 循环
如果我们需要给一个循环里面的元素添加组件,如给10个li添加单击事件,等重复编译一个组件内容的时候,我们需要在return中添加行为:
1 <body ng-app="MyApp"> 2 <repeat-ntimes repeat="10"> 3 <h1>Header 1</h1> 4 <p>This is the paragraph.</p> 5 </repeat-n-times> 6 7 var app = angular.module("MyApp", []); 8 9 app.directive("repeatNtimes", function() { 10 11 return { 12 restrict: "E", 13 14 compile: function(tElement, attrs) { 15 var content = tElement.children(); 16 for (var i=1; i<attrs.repeat; i++) { 17 tElement.append(content.clone()); 18 } 19 //当你在重复编译一个组件内容的时候,你需要在里return一个行为。 20 return function (scope, element, attrs) { 21 element.on("click", "h1", function() { 22 $(this).css({ "background-color": "red" }); 23 }); 24 }; 25 } 26 }; 27 });
4. require 模块引用
当我们需要在一个一个中加载其他控制器的属性或者变量的时候,就可以用require。
1 var app = angular.module("MyApp", []); 2 3 app.directive("basket", function() { 4 return { 5 restrict: "E", 6 controller: function($scope, $element, $attrs) { 7 $scope.content = []; 8 9 this.addApple = function() { 10 $scope.content.push("apple"); 11 }; 12 13 this.addOrange = function() { 14 $scope.content.push("orange"); 15 }; 16 }, 17 link: function(scope, element) { 18 element.bind("mouseenter", function() { 19 console.log(scope.content); 20 }); 21 } 22 }; 23 }); 24 25 app.directive("apple", function() { 26 return { 27 require: "basket", 28 link: function(scope, element, attrs, basketCtrl) { 29 basketCtrl.addApple(); 30 } 31 }; 32 }); 33 34 app.directive("orange", function() { 35 return { 36 require: "basket", 37 link: function(scope, element, attrs, basketCtrlaa) { 38 basketCtrlaa.addOrange(); 39 } 40 }; 41 });
可以看到上面代码中的require连接的就是basket控制器,这个控制器也是第一段代码声明的。这里请注意link函数的第四个参数basketCtrl,这个参数代表的是引用的控制器,可以看到名字可以随便起但是最好遵循XXctrl这种起名方式。