溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

angular的scopel指令如何使用

發(fā)布時(shí)間:2022-03-15 14:39:47 來(lái)源:億速云 閱讀:152 作者:iii 欄目:web開發(fā)

這篇文章主要介紹了angular的scopel指令如何使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇angular的scopel指令如何使用文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

使用代碼如下:

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }</style></head><bodyng-app="myApp">

  <divng-controller="mainCtrl">

  <my-btn></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp.controller('mainCtrl',['$scope',function($scope){

  $scope.myClass='primary';

  }]);

  myApp.directive('myBtn',function(){return{

  template:'<inputtype="button"value="按鈕"class="{{myClass}}">'

  }

  });</script></body></html>

  1.png

  使用自定義指令像上面一樣的確不錯(cuò),但是如果你想要對(duì)每一個(gè)指令渲染出來(lái)的按鈕定制化,則好像不可以,比如下面我們創(chuàng)建一堆這個(gè)自定義指令,他們長(zhǎng)得一模一樣:

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }</style></head><bodyng-app="myApp">

  <divng-controller="mainCtrl">

  <my-btn></my-btn>

  <my-btn></my-btn>

  <my-btn></my-btn>

  <my-btn></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp.controller('mainCtrl',['$scope',function($scope){

  $scope.myClass='primary';

  }]);

  myApp.directive('myBtn',function(){return{

  template:'<inputtype="button"value="按鈕"class="{{myClass}}">'

  }

  });</script></body></html>

  2.png

  一種思路是把這幾個(gè)自定義的指令按鈕放到不同的控制器里面,然后控制器里通過(guò)$scope上下文傳遞不同的值:

  <!DOCTYPEhtml><html><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:green;

  }.default{background:gray;

  }</style></head><bodyng-app="myApp">

  <divng-controller="aCtrl">

  <my-btn></my-btn>

  </div>

  <divng-controller="bCtrl">

  <my-btn></my-btn>

  </div>

  <divng-controller="cCtrl">

  <my-btn></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp.controller('aCtrl',['$scope',function($scope){

  $scope.myClass='primary';

  }]);

  myApp.controller('bCtrl',['$scope',function($scope){

  $scope.myClass='success';

  }]);

  myApp.controller('cCtrl',['$scope',function($scope){

  $scope.myClass='default';

  }]);

  myApp.directive('myBtn',function(){return{

  template:'<inputtype="button"value="按鈕"class="{{myClass}}">'

  }

  });</script></body></html>

  3.png

  這樣寫太麻煩了,所以我們的angular為我們的自定義指令提供了一個(gè)配置項(xiàng)叫scope,所以,我們可以如下這樣寫:

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:green;

  }.default{background:gray;

  }</style></head><bodyng-app="myApp">

  <divng-controller="Controller">

  <my-btnb="className1"></my-btn>

  <my-btnb="className2"></my-btn>

  <my-btnb="className3"></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp

  .controller('Controller',['$scope',function($scope){

  $scope.className1='primary';

  $scope.className2='success';

  $scope.className3='default';

  }])

  .directive('myBtn',function(){return{

  scope:{

  a:'=b'

  },

  template:'<inputtype="button"value="按鈕"class="{{a}}">'

  }

  });</script></body></html>

  要看懂上面的只要注意兩點(diǎn):

  這里的獨(dú)立作用域里面的a代表的是template里面的模型a

  =b代表的是要angular去尋找視圖里面的當(dāng)前指令的屬性b

  屬性b的值需要去外部作用域里面去尋找

  如果你想在指令作用域里綁定的模型的名字和外部使用的時(shí)候的屬性名一樣,可以省寫成如下:

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:green;

  }.default{background:gray;

  }</style></head><bodyng-app="myApp">

  <divng-controller="Controller">

  <my-btna="className1"></my-btn>

  <my-btna="className2"></my-btn>

  <my-btna="className3"></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp

  .controller('Controller',['$scope',function($scope){

  $scope.className1='primary';

  $scope.className2='success';

  $scope.className3='default';

  }])

  .directive('myBtn',function(){return{

  scope:{

  a:'='

  },

  template:'<inputtype="button"value="按鈕"class="{{a}}">'

  }

  });</script></body></html>

  當(dāng)然,上面的=號(hào)是雙向數(shù)據(jù)綁定:

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:green;

  }.default{background:gray;

  }</style></head><bodyng-app="myApp">

  <divng-controller="Controller">

  <my-btna="abc"></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp

  .controller('Controller',['$scope',function($scope){

  $scope.abc='我是初始內(nèi)容';

  }])

  .directive('myBtn',function(){return{

  scope:{

  a:'='

  },

  template:'<inputtype="text"ng-model="a"><span>{{a}}</span>'

  }

  });</script></body></html>

  如果只是想單向的數(shù)據(jù)通信,可以用@符號(hào):

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:red;

  }.default{background:red;

  }</style></head><bodyng-app="myApp">

  <divng-controller="Controller">

  <my-btna="primary"></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp

  .controller('Controller',['$scope',function($scope){

  $scope.mm='primary';

  }])

  .directive('myBtn',function(){return{

  scope:{

  a:'@'

  },

  template:'<inputtype="button"value="按鈕"class="{{a}}">'

  }

  });</script></body></html>

  如果想用ng-class,也是可以的:

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:red;

  }.default{background:red;

  }</style></head><bodyng-app="myApp">

  <divng-controller="Controller">

  <my-btna="primary"></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp

  .controller('Controller',['$scope',function($scope){

  $scope.mm=true;

  }])

  .directive('myBtn',function(){return{

  scope:{

  a:'@'

  },

  template:'<inputtype="button"value="按鈕"ng-class="{primary:a}">'

  }

  });</script></body></html>

  最后,還有一個(gè)scope可以設(shè)置是引用外部作用域的方法

  <!DOCTYPEhtml><htmllang="en"><head>

  <metacharset="UTF-8">

  <title>Document</title>

  <style>

  .primary{background:red;

  }.success{background:red;

  }.default{background:red;

  }</style></head><bodyng-app="myApp">

  <divng-controller="Controller">

  <my-btnfn2="fn()"></my-btn>

  </div>

  <scriptsrc="node_modules/angular/angular.min.js"></script>

  <script>

  varmyApp=angular.module('myApp',[]);

  myApp

  .controller('Controller',['$scope',function($scope){

  $scope.fn=function(){

  alert(11);

  }

  }])

  .directive('myBtn',function(){return{

  scope:{

  fn1:'&fn2'

  },

  template:'<inputtype="button"value="按鈕"ng-click="fn1()">'

  }

  });</script></body></html>

關(guān)于“angular的scopel指令如何使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“angular的scopel指令如何使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI