angularjs學習系(3)指令的@=&,angularjs指令

來源:互聯網
上載者:User

angularjs學習系(3)指令的@=&,angularjs指令

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>'}});

如果logchore帶有參數,

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>'}});


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.