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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:18:30
字體:
來源:轉載
供稿:網友

引入主題背景:angular 的指令配置中的template可以直接使用硬編碼寫相應的代碼,不過也可以根據變量,進行動態更新。那么需要用到那些變量,因用法的不同,所以需要設置合適的綁定策略。

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的@對應的,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>'     } }); 

在輸入框輸入數字會綁定到指令模板的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:最后說&,這個是用來方法調用的

angularjs.html代碼如下:

<!doctype html> <html ng-app='myApp'>  <head>    </head>  <body>    <div ng-controller="listCtrl">    <kid flavor="logchore()" ></kid> //先比=,函數賦值的形式,而logchore函數必須是域scope下聲明的函數 </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>'     } });             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 舒城县| 临安市| 普格县| 高陵县| 天柱县| 临澧县| 沈阳市| 峨边| 门头沟区| 阿坝县| 库车县| 广南县| 出国| 五常市| 梁河县| 陆良县| 綦江县| 乐平市| 葵青区| 永善县| 金山区| 马尔康县| 上犹县| 弥渡县| 石林| 娄烦县| 寿宁县| 宁津县| 广河县| 乌鲁木齐县| 丰县| 凉城县| 固原市| 丘北县| 金乡县| 东台市| 吉安县| 岳西县| 桑植县| 闻喜县| 扎鲁特旗|