加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

学习AngularJs:Directive指令用法(完整版)

发布时间:2016-11-25 10:16:49 所属栏目:Windows 来源:站长网
导读:本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github.com/angular/angular.js/ AngularJs下载地址:https://angularjs.org/ 摘要:Directive(指令)笔者认为是AngularJ非常强大而有有用的功能之一。它就相当于为我们写了公共的自定义DOM元

(2)一个函数,可接受两个参数tElement和tAttrs
其中tElement是指使用此指令的元素,而tAttrs则实例的属性,它是一个由元素上所有的属性组成的集合(对象)形如:
lt;hello-world2 title = '我是第二个directive'gt;lt;/hello-world2gt;nbsp;
其中title就是tattrs上的属性

下面让我们看看template是一个函数时候的情况

lt;!DOCTYPE htmlgt; 
lt;html lang="zh" ng-app="myApp"gt; 
lt;headgt; 
 lt;meta charset="UTF-8"gt; 
 lt;titlegt;AngularJS入门学习lt;/titlegt; 
 lt;script type="text/javascript" src="./1.5.3/angular.min.js"gt;lt;/scriptgt; 
lt;/headgt; 
lt;bodygt; 
lt;hello-worldgt;lt;/hello-worldgt; 
lt;hello-world2 title = '我是第二个directive'gt;lt;/hello-world2gt; 
lt;/bodygt; 
lt;script type="text/javascript"gt; 
var app = angular.module('myApp', []); 
app.directive('helloWorld', function() { 
 return { 
 restrict: 'E', 
 template: 'lt;divgt;lt;h1gt;Hi 我是林炳文~~~lt;/h1gt;lt;/divgt;', 
 replace: true 
 }; 
}); 
app.directive("helloWorld2",function(){ 
 return{ 
 restrict:'EAC', 
 template: function(tElement,tAttrs){ 
 var _html = ''; 
 _html += 'lt;divgt;' +'hello '+tAttrs.title+'lt;/divgt;'; 
 return _html; 
 } 
 }; 
 }); 
lt;/scriptgt; 
lt;/htmlgt; 

结果:

学习AngularJs:Directive指令用法(完整版)

可以看到指令中还用到了hello-world2中的标签中的 title字段

5.templateUrl(字符串或者函数),可选参数,可以是
(1)一个代表HTML文件路径的字符串

(2)一个函数,可接受两个参数tElement和tAttrs(大致同上)

注意:在本地开发时候,需要运行一个服务器,不然使用templateUrl会报错 Cross Origin Request Script(CORS)错误。由于加载html模板是通过异步加载的,若加载大量的模板会拖慢网站的速度,这里有个技巧,就是先缓存模板
你可以再你的index页面加载好的,将下列代码作为你页面的一部分包含在里面。

lt;script type='text/ng-template' id='hello.html'gt; 
 lt;divgt;lt;h1gt;Hi 我是林炳文~~~lt;/h1gt;lt;/divgt; 
lt;/scriptgt; 

这里的id属性就是被设置在templateUrl上用的。

lt;!DOCTYPE htmlgt; 
lt;html lang="zh" ng-app="myApp"gt; 
lt;headgt; 
 lt;meta charset="UTF-8"gt; 
 lt;titlegt;AngularJS入门学习lt;/titlegt; 
 lt;script type="text/javascript" src="./1.5.3/angular.min.js"gt;lt;/scriptgt; 
lt;/headgt; 
lt;bodygt; 
lt;hello-worldgt;lt;/hello-worldgt; 
lt;/bodygt; 
lt;script type="text/javascript"gt; 
var app = angular.module('myApp', []); 
app.directive('helloWorld', function() { 
 return { 
 restrict: 'E', 
 templateUrl: 'hello.html', 
 replace: true 
 }; 
}); 
lt;/scriptgt; 
lt;script type='text/ng-template' id='hello.html'gt; 
 lt;divgt;lt;h1gt;Hi 我是林炳文~~~lt;/h1gt;lt;/divgt; 
lt;/scriptgt; 
lt;/htmlgt; 

输出结果:

学习AngularJs:Directive指令用法(完整版)

另一种办法缓存是:

app.run(["$templateCache", function($templateCache) { 
 $templateCache.put("hello.html", 
 "lt;divgt;lt;h1gt;Hi 我是林炳文~~~lt;/h1gt;lt;/divgt;"); 
}]); 

使用实例如下:

lt;!DOCTYPE htmlgt; 
lt;html lang="zh" ng-app="myApp"gt; 
lt;headgt; 
 lt;meta charset="UTF-8"gt; 
 lt;titlegt;AngularJS入门学习lt;/titlegt; 
 lt;script type="text/javascript" src="./1.5.3/angular.min.js"gt;lt;/scriptgt; 
lt;/headgt; 
lt;bodygt; 
lt;hello-worldgt;lt;/hello-worldgt; 
lt;/bodygt; 
lt;script type="text/javascript"gt; 
var app = angular.module('myApp', []); 
app.directive('helloWorld', function() { 
 return { 
 restrict: 'E', 
 templateUrl: 'hello.html', 
 replace: true 
 }; 
}); 
app.run(["$templateCache", function($templateCache) { 
 $templateCache.put("hello.html", 
 "lt;divgt;lt;h1gt;Hi 我是林炳文~~~lt;/h1gt;lt;/divgt;"); 
}]); 
lt;/scriptgt; 
lt;/htmlgt; 

结果:

学习AngularJs:Directive指令用法(完整版)

nbsp;其实第一种方法还好一些,写起来会比较快,笔者就得最多的也是第一种写法,直接包在scprit当中

nbsp;6.replace
(布尔值),默认值为false,设置为true时候,我们再来看看下面的例子(对比下在template时候举的例子)

nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;学习AngularJs:Directive指令用法(完整版)nbsp;nbsp;nbsp;nbsp;nbsp;

nbsp;replace为true时,hello-world这个标签不在了,反之,则存在。

7.scope
(1)默认值false。表示继承父作用域;

(2)true。表示继承父作用域,并创建自己的作用域(子作用域);

(3){}。表示创建一个全新的隔离作用域;

7.1首先我们先来了解下scope的继承机制。我们用ng-controller这个指令举例,

lt;!DOCTYPE htmlgt; 
lt;html lang="zh" ng-app="myApp"gt; 
lt;headgt; 
 lt;meta charset="UTF-8"gt; 
 lt;titlegt;AngularJS入门学习lt;/titlegt; 
 lt;script type="text/javascript" src="./1.5.3/angular.min.js"gt;lt;/scriptgt; 
lt;/headgt; 
lt;bodygt; 
lt;div ng-controller='MainController'gt; 
 父亲:{{name}}lt;input ng-model="name" /gt; 
 lt;div my-directivegt;lt;/divgt; 
 lt;/divgt; 
lt;/bodygt; 
lt;script type="text/javascript"gt; 
var app = angular.module('myApp', []); 
app.controller('MainController', function ($scope) { 
 $scope.name = '林炳文'; 
}); 
app.directive('myDirective', function () { 
 return { 
 restrict: 'EA', 
 scope:false, 
 template: 'lt;divgt;儿子:{{ name }}lt;input ng-model="name"/gt;lt;/divgt;' 
 }; 
}); 
lt;/scriptgt; 
lt;/htmlgt; 

接下来我们通过一个简单明了的例子来说明scope取值不同的差别:

scope:false

学习AngularJs:Directive指令用法(完整版)

scope:true

学习AngularJs:Directive指令用法(完整版)

scope:{}

学习AngularJs:Directive指令用法(完整版)

当为false时候,儿子继承父亲的值,改变父亲的值,儿子的值也随之变化,反之亦如此。(继承不隔离)

当为true时候,儿子继承父亲的值,改变父亲的值,儿子的值随之变化,但是改变儿子的值,父亲的值不变。(继承隔离)

当为{}时候,没有继承父亲的值,所以儿子的值为空,改变任何一方的值均不能影响另一方的值。(不继承隔离)

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读