国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

angularjs指令之綁定策略(@、=、&)

2019-11-19 16:50:00
字體:
供稿:網(wǎng)友

引入主題背景:angular 的指令配置中的template可以直接使用硬編碼寫相應(yīng)的代碼,不過也可以根據(jù)變量,進行動態(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 环江| 文登市| 临西县| 丹江口市| 简阳市| 云梦县| 留坝县| 长岛县| 黄龙县| 台东市| 抚宁县| 凤山县| 涪陵区| 兴城市| 前郭尔| 扶沟县| 黑龙江省| 忻州市| 昭苏县| 昌乐县| 平远县| 仁怀市| 潞城市| 盱眙县| 墨竹工卡县| 孝义市| 北票市| 杂多县| 凤凰县| 海安县| 祁连县| 醴陵市| 阿拉善盟| 怀集县| 曲麻莱县| 开平市| 察哈| 尉氏县| 如东县| 石嘴山市| 洞口县|