Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

对于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="@H_502_67@{{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>@H_502_67@{{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',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',function($scope){  
   $scope.logchore=function(){  
        alert('ok');  
   };  
});  


myApp.directive('test',scope:{  
            flavor:"&"    
        },template:'<div ><button ng-click="flavor()"></button></div>'  

    }  
});

尝试一下,就明白了,简洁明了!

大佬总结

以上是大佬教程为你收集整理的AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)全部内容,希望文章能够帮你解决AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。