angularjs學習筆記3-directive中scope的綁定修飾符

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   使用   java   sp   

在angularjs中,一個directive返回一個對象,對象存在很多屬性,並且可以在directive中自訂自己的scope,而使用自己的scope是為了防止一個directive被使用在多個地方之間發生相互影響,通常這個scope可以被定義為true或者對象,當這個scope被定義為true時,代表這個指令擁有獨立的scope,它被多次使用在多個地方之間不會相互影響,例如:

HTML代碼

<!doctype html><html ng-app="MyModule">    <head>        <meta charset="utf-8">        <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css">    </head>    <body>        <hello></hello>        <hello></hello>            </body>    <script src="framework/angular-1.3.0.14/angular.js"></script>    <script src="IsolateScope.js"></script></html>

js代碼

var myModule = angular.module("MyModule", []);myModule.directive("hello", function() {    return {        restrict: ‘AE‘,        //這裡是建立了一個獨立的scope,以防止各個指令之間相互影響        scope:true,        template: ‘<div><input type="text" ng-model="userName"/>{{userName}}</div>‘,        replace: true    }});

 示範效果:

你會發現這兩個指令之間輸入沒有互相影響。當你不指定這個directive中的scope屬性的時候,例如這樣

var myModule = angular.module("MyModule", []);myModule.directive("hello", function() {    return {        restrict: ‘AE‘,        //這裡是建立了一個獨立的scope,以防止各個指令之間相互影響       // scope:true,        template: ‘<div><input type="text" ng-model="userName"/>{{userName}}</div>‘,        replace: true    }});

那麼顯示效果如下:

操作其中一個input,那麼任何使用這個指令的地方都會被修改。即這個指令的多個執行個體之間是共用一個 scope的

scope也可以被設定為一個對象,即{},當設定為空白對象{}的時候,效果和設定為true一樣,這裡可以通過將其設定為不為空白的對象以達到複用其對應controller中的屬性及方法。

要複用其對應controller(即其父scope)中的方法和對象,angular提供了三種修飾符:

第一種為:@,這表示綁定的是一個字串,而且是單向綁定

<!doctype html><html ng-app="MyModule">    <head>        <meta charset="utf-8">        <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css">    </head>    <body>        <div ng-controller="MyCtrl">            <drink flavor="{{ctrlFlavor}}"></drink>        </div>    </body>    <script src="framework/angular-1.3.0.14/angular.js"></script>    <script src="ScopeAt.js"></script></html>

js 代碼

var myModule = angular.module("MyModule", []);myModule.controller(‘MyCtrl‘, [‘$scope‘, function($scope){$scope.ctrlFlavor="111";}])myModule.directive("drink", function() {    return {        restrict:‘AE‘,        //這裡的        scope:{            //@表示把當前屬性作為字串進行綁定            flavor:‘@‘        },        template:"<div>{{flavor}}</div>"            }});

 首先這個指令會將HTML中的drink標籤替換為"<div>{{flavor}}</div>",而原HTML的<drink flavor="{{ctrlFlavor}}"></drink>存在一個flavor的屬性,指定其綁定了一個運算式,而directive中又指定了這個運算式是個字串 scope:{flavor:‘@‘},這就將drink標籤的flavor屬性指向了其對應的controller即MyCtrl中的scope中的ctrlFlavor上,這樣directive就複用了其controller中的一個屬性,但是這種複用或者叫綁定是單向的,只有controller發生改變directive內容才會改變,顯示效果如下:

這是第一種修飾符@的作用,簡單來就是讓directive可以單向綁定其父scope中的屬性;

第二種修飾符是=,表示綁定的是其父scope中的屬性,但是是雙向繫結,代碼如下:

<!doctype html><html ng-app="MyModule">    <head>        <meta charset="utf-8">        <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css">    </head>    <body>        <div ng-controller="MyCtrl">            Ctrl:            <br>            <input type="text" ng-model="ctrlFlavor">            <br>            Directive:            <br>            <drink flavor="ctrlFlavor"></drink>        </div>    </body>    <script src="framework/angular-1.3.0.14/angular.js"></script>    <script src="ScopeEqual.js"></script></html>

js代碼:

var myModule = angular.module("MyModule", []);myModule.controller(‘MyCtrl‘, [‘$scope‘, function($scope){ $scope.ctrlFlavor="111";}])myModule.directive("drink", function() {    return {     restrict:‘AE‘,        scope:{         flavor:‘=‘        },        template:‘<input type="text" ng-model="flavor"/>‘    }});

這裡將flavor依然指向的是其controller的scope中的ctrFlavor屬性,但是進行模板替換的時候使用的是ng-model 用來進行雙向資料操作,注意到 scope:{flavor:‘=‘},這說明這個directive的flavor屬性進行了一個雙向繫結,而對應的html中不再是使用{{}}這種單向綁定了,而是直接將這個<drink flavor="ctrlFlavor"></drink>屬性指向了controller中的scope中的ctrlFlavor屬性,簡單來講,就是利用=修飾符,你可以讓你的directive與其父範圍中的某個屬性進行雙向繫結;

第三種修飾符是&,它的含義是用來綁定directive父範圍中的某個函數,代碼如下:

<!doctype html><html ng-app="MyModule"> <head>  <meta charset="utf-8">  <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css"> </head> <body>  <div ng-controller="MyCtrl">   <greeting greet="sayHello(name)"></greeting>   <greeting greet="sayHello(name)"></greeting>   <greeting greet="sayHello(name)"></greeting>  </div> </body> <script src="framework/angular-1.3.0.14/angular.js"></script> <script src="ScopeAnd.js"></script></html>

js代碼

var myModule = angular.module("MyModule", []);myModule.controller(‘MyCtrl‘, [‘$scope‘, function($scope){ $scope.sayHello=function(name){  alert("Hello "+name); }}])myModule.directive("greeting", function() {    return {     restrict:‘AE‘,        scope:{         greet:‘&‘        },        template:‘<input type="text" ng-model="userName" /><br/>‘+           ‘<button class="btn btn-default" ng-click="greet({name:userName})">Greeting</button><br/>‘    }});

在這裡,directive中的scope中設定一個屬性為 scope:{ greet:‘&‘},&代表這個指令的greet屬性綁定了其父scope中的一個函數,而在HTML中<greeting greet="sayHello(name)"></greeting>greet指向的是其父scope(即controller)的sayHello方法,並傳遞一個參數name,同時在模板替換中,將ng-click設定為greet屬性對應的值,這裡傳遞的參數是一個對象ng-click="greet({name:userName})",這種寫法是用來收集input中輸入的值。

 

 

 

 

 

 


 

 

angularjs學習筆記3-directive中scope的綁定修飾符

聯繫我們

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