AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)
来源:互联网 发布:asp.net 高级编程 编辑:程序博客网 时间:2024/05/08 16:54
对于HTML5的兴起,Angular火热绝对值得称赞,外加庞大的社区做支持,是的Angular的普及度越来越高,称为最为流行的html5框架。
对于一个html5框架的好坏,我们有几个评判标准, 轻量级,可拓展,易复用,速度快。
对组件复用这点,angular以directive的形式展示给开发者,是一个还算不错的选择,作为一个UI组件,必定存在数据交互。
那么数据交互过程中的几个符号我们一定要有所了解,以及他们的区别是什么,防止我们在运用过程中出错。
1. 首先,我们看一scope作用域下面@的使用:
html
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <input type="text" ng-model="t" /> <test title="{{t}}" > <span>我的angularjs</span> </test> </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main.js"></script> </body></html>
js
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore="motorola"; }); myApp.directive('test',function(){ return { 'restrict':'E', scope:{ title:"@" }, template:'<div >{{title}}</div>' } });
这个必须指定的,这里的title是指令里scope的@对应的,t就是控制域scope下的 .
2. = 的使用。
html
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <input type="text" ng-model="t" /> <test title="t" > <p>{{title}}</p> <span>我的angularjs</span> </test> </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html>
js
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore="motorola"; }); myApp.directive('test',function(){ return { 'restrict':'E', scope:{ title:"=" }, template:'<div >{{title}}</div>' } });
和上面@相比,这个直接赋值等于scope域下的t了
3. 最好我们看看&符号的使用
html
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <test flavor="logchore()" ></test> </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html>
js
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore=function(){ alert('ok'); }; }); myApp.directive('test',function(){ return { 'restrict':'E', scope:{ flavor:"&" }, template:'<div ><button ng-click="flavor()"></button></div>' } });
尝试一下,就明白了,简洁明了!
0 0
- AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)
- AngularJS1.X学习笔记9-自定义指令(中)
- angularJS1自定义directive指令的创建方式
- angularJs中directive的scope ‘@’,‘=’区别
- 使用angularjs1.x构建前台开发框架(二)——require的使用
- AngularJS1.X学习笔记5-加强版的表单
- 使用angularjs1.x构建前台开发框架(五)——掩膜的实现
- 使用angularjs1.x构建前台开发框架(六)——service的引用
- AngularJS1.x学习(仿微信图片手势缩放效果实现)
- AngularJS1.X学习笔记8-自定义指令(上)
- AngularJS1.X学习笔记10-自定义指令(下)
- velocity中directive的使用
- AngularJS1.X学习笔记1-整体看看
- AngularJS1.X学习笔记2-数据绑定
- AngularJS1.X学习笔记7-过滤器
- AngularJS1.X学习笔记11-服务
- AngularJS1.X学习笔记12-Ajax
- AngularJS1.X学习笔记13-路由
- SpringMVC的Handler处理及url映射
- POI 解析Excel表格实例
- UVA - 123 Searching Quickly
- UVA - 755 487--3279
- springmvc-ajax-jquery 异步校验登录【亲测可用】
- AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)
- C语言fputc()函数:写文件函数(将一指定字符写入文件流中)
- UVA - 10785 The Mad Numerologist
- 检测乱码
- 个人渗透笔记
- 给定两个字符串,重新排列后,两个是否相等
- UVA - 575 Skew Binary
- live555 源代码简单分析1:主程序
- UVA - 10110 Light, more light