详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2300字,纯文字阅读大概需要4分钟。
内容图文
![详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用](/upload/InfoBanner/zyjiaocheng/342/79d79842bd554d6a9f1444fdabae3300.jpg)
对于一个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>' } });
尝试一下,就明白了,简洁明了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
内容总结
以上是互联网集市为您收集整理的详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用全部内容,希望文章能够帮你解决详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。