詳解AngularJS1.x學(xué)習(xí)directive 中‘& ’‘=’ ‘@’符號(hào)的區(qū)別使用
對(duì)于一個(gè)Html5框架的好壞,我們有幾個(gè)評(píng)判標(biāo)準(zhǔn), 輕量級(jí),可拓展,易復(fù)用,速度快。
對(duì)組件復(fù)用這點(diǎn),angular以directive的形式展示給開發(fā)者,是一個(gè)還算不錯(cuò)的選擇,作為一個(gè)UI組件,必定存在數(shù)據(jù)交互。
那么數(shù)據(jù)交互過程中的幾個(gè)符號(hào)我們一定要有所了解,以及他們的區(qū)別是什么,防止我們?cè)谶\(yùn)用過程中出錯(cuò)。
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>'
}
});
這個(gè)必須指定的,這里的title是指令里scope的@對(duì)應(yīng)的,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>'
}
});
和上面@相比,這個(gè)直接賦值等于scope域下的t了
3. 最好我們看看&符號(hào)的使用
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>'
}
});
嘗試一下,就明白了,簡(jiǎn)潔明了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 學(xué)習(xí)AngularJs:Directive指令用法(完整版)
- AngularJS中的Directive實(shí)現(xiàn)延遲加載
- AngularJS入門心得之directive和controller通信過程
- AngularJS中directive指令使用之事件綁定與指令交互用法示例
- Angular之指令Directive用法詳解
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- AngularJS directive返回對(duì)象屬性詳解
- AngularJS中的Directive自定義一個(gè)表格
- Angular 根據(jù) service 的狀態(tài)更新 directive
- AngularJs directive詳解及示例代碼
相關(guān)文章
AngularJs unit-testing(單元測(cè)試)詳解
本文主要介紹AngularJs unit-testing(單元測(cè)試)的內(nèi)容,這里整理了單元測(cè)試的知識(shí),及示例代碼,有興趣的朋友可以參考下2016-09-09
快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器
任何時(shí)候,如果我們想要為請(qǐng)求添加全局功能,例如身份認(rèn)證、錯(cuò)誤處理等,在請(qǐng)求發(fā)送給服務(wù)器之前或服務(wù)器返回時(shí)對(duì)其進(jìn)行攔截,是比較好的實(shí)現(xiàn)手段2015-12-12
AngularJS創(chuàng)建一個(gè)上傳照片的指令實(shí)例代碼
這篇文章主要介紹了AngularJS創(chuàng)建一個(gè)上傳照片的指令實(shí)例代碼,需要的朋友可以參考下2018-02-02
Angular2管道Pipe及自定義管道格式數(shù)據(jù)用法實(shí)例分析
這篇文章主要介紹了Angular2管道Pipe及自定義管道格式數(shù)據(jù)用法,結(jié)合實(shí)例形式詳細(xì)分析了Angular2管道與純管道相關(guān)概念、語(yǔ)法及使用技巧,需要的朋友可以參考下2017-11-11
AngularJs定制樣式插入到ueditor中的問題小結(jié)
這篇文章主要介紹了AngularJs定制樣式插入到ueditor中的問題小結(jié)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
詳解angularJs中自定義directive的數(shù)據(jù)交互
這篇文章主要介紹了詳解angularJs中自定義directive的數(shù)據(jù)交互,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-01-01
走進(jìn)AngularJs之過濾器(filter)詳解
本篇文章主要介紹了AngularJs之過濾器(filter)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-02-02

