angular之 ——自定义指令(dirctive)
来源:互联网 发布:阮星竹 知乎 编辑:程序博客网 时间:2024/06/11 04:16
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body ng-app="myApp"> <hello> 111 </hello> <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> angular.module("myApp", []) .directive("hello", function() { return { restrict: 'ACE', template: "<div>hello angular<span ng-transclude></span></div>", replace: true, transclude: true } }) .controller("myController", ["$scope", function($scope) { }]) </script> </body> </html>
> 结果:hello angular 111* link:操作dom元素,处理指令内部的逻辑,包含scope、element、attrs、父控制器四个形参* require:指定要依赖的父指令,格式为”^父dirctive”* controller:自定义指令的控制器,将要暴露的逻辑写在里面指令之间的交互效果如下:<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script> </head> <body ng-app="myApp"> <surpermen strong>1111</surpermen> <surpermen strong speed>1111</surpermen> <surpermen strong speed light>222</surpermen> <script type="text/javascript"> var myApp = angular.module("myApp",[]) myApp.directive("surpermen",function(){ return { restrict:"E", scope:{},//独立作用域 controller:["$scope",function($scope){ //this指向controller $scope.asibility = []; this.addStrong = function(){ $scope.asibility.push("strong"); }; this.addSpeed = function(){ $scope.asibility.push("speed"); } this.addLight = function(){ $scope.asibility.push("light"); } }], link:function(scope,element,attr){ element.on("mouseenter",function(){ console.log(scope.asibility) }) } } }) .directive("strong",function(){ return { restrict:"A", require:"^surpermen", link:function(scope,element,attrs,surpermenCtrl){ surpermenCtrl.addStrong(); } } }) .directive("speed",function(){ return { restrict:"A", require:"^surpermen", link:function(scope,element,attrs,surpermenCtrl){ surpermenCtrl.addSpeed(); } } }) .directive("light",function(){ return { restrict:"A", require:"^surpermen", link:function(scope,element,attrs,surpermenCtrl){ surpermenCtrl.addLight(); } } }) </script> </body></html>
* scope:当为{},则为每个指令定义独立作用域,即隔离scope。scope中的取值如下图所示 **什么是隔离scope?** > AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性。通常使用这种直接共享的方式可以实现一些简单的 directive 功能。当你需要创建一个可重复使用的 directive,只是偶尔需要访问或者修改父 scope 的数据,就需要使用隔离 scope。当使用隔离 scope 的时候,directive 会创建一个没有依赖父 scope 的 scope,并提供一些访问父 scope 的方式。共享scope:js代码 app.controller("myController", function ($scope) { $scope.name = "hello world"; }).directive("shareDirective", function () { return { template: 'Say:{{name}}' }});
html:<div ng-controller="myController"> <div share-directive=""></div></div>
结果:Say:hello world隔离scopejs代码:app.controller("myController", function ($scope) { $scope.name = "hello world";}).directive("isolatedDirective", function () { return { scope: {}, template: 'Say:{{name}}' }});
html代码<div ng-controller="myController"> <div isolated-directive=""></div></div>
结果:Say:由以上例子可以看出:**共享 scope 允许从父 scope 渗入到 directive 中,而隔离 scope 不能,在隔离 scope 下,给 directive 创造了一堵墙,使得父 scope 无法渗入到 directive 中。**那隔离的scope要跟父scope交互怎么办?directive 在使用隔离 scope 的时候,提供了三种方法同隔离之外的地方交互。这三种分别是- @ 绑定一个局部 scope 属性到当前 dom 节点的属性值。结果总是一个字符串,因为 dom 属性是字符串。- & 提供一种方式执行一个表达式在父 scope 的上下文中。如果没有指定 attr 名称,则属性名称为相同的本地名称。- = 通过 directive 的 attr 属性的值在局部 scope 的属性和父 scope 属性名之间建立双向绑定。@属性:js代码: app.controller("myController", function ($scope) { $scope.name = "hello world"; }).directive("isolatedDirective", function () { return { scope: { name: "@" }, template: 'Say:{{name}} <br>改变隔离scope的name:<input type="buttom" value="" ng-model="name" class="ng-pristine ng-valid">' }})
html代码:<div ng-controller="myController"> <div class="result"> <div>父scope: <div>Say:{{name}}<br>改变父scope的name:<input type="text" value="" ng-model="name"/></div> </div> <div>隔离scope: <div isolated-directive name="{{name}}"></div> </div> <div>隔离scope(不使用{{name}}): <div isolated-directive name="name"></div> </div> </div> ```= 属性: js代码:```javascript app.controller("myController", function ($scope) { $scope.user = { name: 'hello', id: 1 }; }).directive("isolatedDirective", function () { return { scope: { user: "=" }, template: 'Say:{{user.name}} <br>改变隔离scope的name:<input type="buttom" value="" ng-model="user.name"/>' } })<div class="se-preview-section-delimiter"></div>
“`html 改变父scope的name:
“`
& 属性:
& 方式提供一种途经是 directive 能在父 scope 的上下文中执行一个表达式。此表达式可以是一个 function。
比如当你写了一个 directive,当用户点击按钮时,directive 想要通知 controller,controller 无法知道 directive 中发生了什么,也许你可以通过使用 angular 中的 event 广播来做到,但是必须要在 controller 中增加一个事件监听方法。
最好的方法就是让 directive 可以通过一个父 scope 中的 function,当 directive 中有什么动作需要更新到父 scope 中的时候,可以在父 scope 上下文中执行一段代码或者一个函数。
js代码
app.controller("myController", function ($scope) { $scope.value = "hello world"; $scope.click = function () { $scope.value = Math.random(); }; }).directive("isolatedDirective", function () { return { scope: { action: "&" }, template: '<input type="button" value="在directive中执行父scope定义的方法" ng-click="action()"/>' } })<div class="se-preview-section-delimiter"></div>
<div ng-controller="myController"> <div>父scope: <div>Say:{{value}}</div> </div> <div>隔离scope: <div isolated-directive action="click()"></div> </div></div>
结果:
![enter description here][4]
改变父scope的name:
“`
& 属性:
& 方式提供一种途经是 directive 能在父 scope 的上下文中执行一个表达式。此表达式可以是一个 function。
比如当你写了一个 directive,当用户点击按钮时,directive 想要通知 controller,controller 无法知道 directive 中发生了什么,也许你可以通过使用 angular 中的 event 广播来做到,但是必须要在 controller 中增加一个事件监听方法。
最好的方法就是让 directive 可以通过一个父 scope 中的 function,当 directive 中有什么动作需要更新到父 scope 中的时候,可以在父 scope 上下文中执行一段代码或者一个函数。
js代码
“`javascript
app.controller(“myController”, function (scope) {scope.value = “hello world”;
scope.click = function () {scope.value = Math.random();
};
}).directive(“isolatedDirective”, function () {
return {
scope: {
action: “&”
},
template: ‘’
}
})
```html <div ng-controller="myController"> <div>父scope: <div>Say:{{value}}</div> </div> <div>隔离scope: <div isolated-directive action="click()"></div> </div></div>
- angular之 ——自定义指令(dirctive)
- Angular自定义指令(初级)
- angular自定义指令(中)
- angular 中的自定义指令之详解API
- 【Angular】——指令
- angular指令学习(三)--自定义指令之compile、link的学习
- angular自定义指令(必填标志)
- angular 自定义指令
- angular js自定义指令
- Angular自定义指令(进阶)
- angular自定义指令
- 创建angular自定义指令
- Angular --- 自定义指令更新
- angular 自定义指令
- angular自定义指令templateUrl
- Angular 自定义指令详解
- 一,Angular自定义指令
- Angular自定义指令
- 【算法】最大窗口
- 什么是Code Review 代码审查
- 【算法】构造数组的MaxTree
- apache2部署访问yaaw
- Python挑战第三题
- angular之 ——自定义指令(dirctive)
- 动态规划:求解最长递增子序列(LIS)
- 【算法】最大矩阵的大小
- linux自动运维(四)-python3的Django环境安装
- 引用队列(ReferenceQueue)浅析
- 【算法】最大值和最小值的差小于或者等于num的子数组数量
- 详解java类的生命周期
- C语言中的链接类型:
- 调试图形(Shader)程序的实战演练,运用普通调试和Visual Studio Graphics Debugger(在VS2015环境下)