angularjs學習之八(angularjs中isolate scope的使用),angularjsisolate

來源:互聯網
上載者:User

angularjs學習之八(angularjs中isolate scope的使用),angularjsisolate

angular js中指令directive有個特別有用的東西,那就是 isolate scope (被隔離的scope)

關於具體他和全域的scope 有什麼區別,可以參考下面這篇博文:

AngularJS 全域scope與Isolate scope通訊

本文主要講解 其具體的幾種使用方式:

1. = 的使用

[html] <div class="card" ng-repeat="app in apps">           <app-info info="app"></app-info>         </div>     [js]     app.directive('appInfo', function() {   return { restrict: 'E', scope: {   info: '=' //如果是 = 就是info屬性的值 賦給 當前scope.info}, templateUrl: 'js/directives/appInfo.html'   }; });

2. =屬性名稱 的使用

[html]     <div ng-controller="AppCtrl as appctrl">           Ctrl           <input type="text" ng-model="appctrl.ctrlFlavor">           Dir           <div ab="25" drink="123456" flavor="appctrl.ctrlFlavor"></div>        </div>        [js]var app = angular.module("drinkApp", []);app.controller("AppCtrl", function() {var appctrl = this; appctrl.ctrlFlavor = "blackberry";});app.directive("drink", function() {return {scope: {flavor: "=ab"},template: '<input type="text" ng-model="flavor">'};});
顯示結果:


3. @ = 和 & 的綜合使用

html 代碼:

<div class="mainController" ng-app="isolateApp">     <div ng-controller="AppCtrl">     <div class="row">     <character name="Roman Regins" image="img/p1.jpg" movetype="movetype" use-move="getMove(name,movetype,move)" class="col-xs-4"></character> <character  name="Seth Rollins" image="./img/p2.jpg" movetype="movetype" use-move="getMove(name,movetype,move)" class="col-xs-4"></character> <character  name="Dean Ambrose" image="./img/p3.jpg" movetype="movetype" use-move="getMove(name,movetype,move)" class="col-xs-4"></character> </div> </div>  </div>
js控制:
//顯示@ = 和 &綜合的   var app = angular.module('isolateApp',[]);   app.controller("AppCtrl",['$scope',"$element",function($scope,$element){       $scope.getMove = function(name,movetype,move){   console.log(name+'$$$'+movetype+'$$$'+move);   }   $scope.movetypes = ['amove','bavi','cmp4'];           $scope.movetype = $scope.movetypes[0];   }])       .directive("character",function(){       return {   restrict:"E",   scope:{   name:"@",  //@指的是屬性的值賦給name 僅此而已   image:"@",    movetype:"=", //表示類型等於當前屬性的值   useMove:"&" //&表示對應的函數的引用 及該屬性對應的函數別名就是他了   },   controller:"AppCtrl", //只有這裡聲明了 才會將select選項載入進來   replace:true,   templateUrl:"shield_isolate.html"   };   })
模板:
<script type="text/ng-template" id="shield_isolate.html">    <div class="panel panel-default"><div class="panel-body"><div><figure>   <img src="{{image}}">   <figcaption>{{name}}</figcaption></figure></div></div><div>Move:   <input type="text" ng-model="value" class="form-controller"/></div><div>   Select Move Type:   <select ng-model="movetype"   ng-options="movetype for movetype in movetypes">   </select></div><div class="panel-footer clearfix"><div class="btn btn-primary"  ng-click="useMove({name:name,movetype:movetype,move:value})">Action!</div>//這裡的":"前的三個參數分別對應 父函數的三個參數的名稱//":" 後的三個參數則對應 給定值scope 的三個屬性 以便一一對應傳值</div></div></script>

顯示結果:







著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.