AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)

本文介绍了AngularJS1.x中directive的三种数据绑定符号'&'、'='和'@'的使用方法和区别。通过实例展示了它们在数据交互中的不同作用,帮助开发者理解如何在AngularJS中正确地进行组件间的数据绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

对于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>'  

    }  
});  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天外野草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值