angularjs指令之綁定策略(@、=、&)
引入主題背景:angular 的指令配置中的template可以直接使用硬編碼寫相應(yīng)的代碼,不過也可以根據(jù)變量,進(jìn)行動態(tài)更新。那么需要用到那些變量,因用法的不同,所以需要設(shè)置合適的綁定策略。
1:先說指令域scope的@
我覺得描述很費勁,直接用代碼來闡述:
AngularJS.html
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <input type="text" ng-model="t" /> <kid title="{{t}}" > //這個必須指定的,這里的title是指令里scope的@對應(yīng)的,t就是控制域scope下的 <span>我的angularjs</span> </kid> </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html>
main05.js
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore="motorola"; }); myApp.directive('kid',function(){ return { 'restrict':'E', scope:{ title:"@" }, template:'<div >{{title}}</div>' } });
在輸入框輸入數(shù)字會綁定到指令模板的title中。
2:再說說Scope的=
angularjs.html
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <input type="text" ng-model="t" /> <kid title="t" > //和上面相比,這個直接賦值等于scope域下的t了 <p>{{title}}</p> <span>我的angularjs</span> </kid> </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html>
main05.js代碼如下:
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore="motorola"; }); myApp.directive('kid',function(){ return { 'restrict':'E', scope:{ title:"=" }, template:'<div >{{title}}</div>' } });
3:最后說&,這個是用來方法調(diào)用的
angularjs.html代碼如下:
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <kid flavor="logchore()" ></kid> //先比=,函數(shù)賦值的形式,而logchore函數(shù)必須是域scope下聲明的函數(shù) </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html>
main05.js代碼如下:
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore=function(){ alert('ok'); }; }); myApp.directive('kid',function(){ return { 'restrict':'E', scope:{ flavor:"&" }, template:'<div ><button ng-click="flavor()"></button></div>' } });
如果logchore帶有參數(shù),
angularjs.html代碼如下:
<!doctype html> <html ng-app='myApp'> <head> </head> <body> <div ng-controller="listCtrl"> <kid flavor="logchore(t)" ></kid> </div> <script type="text/javascript" src="angular.js"></script> <script type="text/javascript" src="main05.js"></script> </body></html>
main05.js代碼如下:
var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore=function(x){ alert(x); }; }); myApp.directive('kid',function(){ return { 'restrict':'E', scope:{ flavor:"&" }, template:'<div > <input type="text" ng-model="v" /> <button ng-click="flavor({t:v})"></button></div>' } });
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular依賴注入optional?constructor?parameters概念
這篇文章主要為大家介紹了Angular?依賴注入領(lǐng)域里?optional?constructor?parameters?的概念及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Ionic + Angular.js實現(xiàn)圖片輪播的方法示例
圖片輪播在我們?nèi)粘i_發(fā)中是再熟悉不過的了,下面這篇文章主要給大家介紹了Ionic + Angular實現(xiàn)圖片輪播的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-05-05Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解
結(jié)構(gòu)指令通過添加和刪除 DOM 元素來更改 DOM 布局。Angular 中兩個常見的結(jié)構(gòu)指令是 *ngIf 和 *ngFor,下面這篇文章主要給大家介紹了關(guān)于Angular 2.x結(jié)構(gòu)指令的相關(guān)資料,需要的朋友可以參考下。2017-05-05微信+angularJS的SPA應(yīng)用中用router進(jìn)行頁面跳轉(zhuǎn),jssdk校驗失敗問題解決
本文主要介紹微信+angularJS的SPA應(yīng)用中用router進(jìn)行頁面跳轉(zhuǎn),jssdk校驗失敗問題解決,這里提供了詳細(xì)的操作方式,有需要的小伙伴可以參考下2016-09-09Angular實現(xiàn)svg和png圖片下載實現(xiàn)
這篇文章主要介紹了Angular實現(xiàn)svg和png圖片下載實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05AngularJS實踐之使用ng-repeat中$index的注意點
最近通過客戶的投訴主要到在ng-repeat中使用了$index引發(fā)的一個bug,下面一起來看看這個錯誤是如何引發(fā)的, 以及如何避免這種bug產(chǎn)生,然后說說我們從中得到的經(jīng)驗和教訓(xùn)。有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12swiper在angularjs中使用循環(huán)輪播失效的解決方法
今天小編就為大家分享一篇swiper在angularjs中使用循環(huán)輪播失效的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09