angularjs自定義ng-model標(biāo)簽的屬性
有的時(shí)候我們需要為非input類型的元素添加ng-model來實(shí)現(xiàn)雙向的數(shù)據(jù)綁定,從而減少冗余代碼,那么可以嘗試一下的方式
例如:我頁面中使用了contenteditable這個(gè)屬性來實(shí)現(xiàn)用戶可直接編譯的div元素
html:
<style> .text{ margin:0 auto; width:100px; height:50px; border:1px solid red; } </style> </head> <body> <div ng-controller="selectController"> <div ng-repeat="pop in citylist"> <div class="text" contenteditable="true" ng-model="pop.pop"></div> </div> <button ng-click="cs()">輸出新數(shù)據(jù)</button> </div> </body>
但是直接綁定ng-model是肯定得不到數(shù)據(jù)的,這時(shí)就需要為其增加自定義的屬性,如下所示。
js:
<script> var app = angular.module('app', []); app.controller('selectController', function ($scope) { $scope.citylist=[{id:1,pop:"北京"},{id:1,pop:"上海"},{id:1,pop:"廣州"}]; $scope.p={}; $scope.cs=function(){ console.log($scope.citylist); } }).directive('contenteditable', function() {//自定義ngModel的屬性可以用在div等其他元素中 return { restrict: 'A', // 作為屬性使用 require: '?ngModel', // 此指令所代替的函數(shù) link: function(scope, element, attrs, ngModel) { if (!ngModel) { return; } // do nothing if no ng-model // Specify how UI should be updated ngModel.$render = function() { element.html(ngModel.$viewValue || ''); }; // Listen for change events to enable binding element.on('blur keyup change', function() { scope.$apply(readViewText); }); // No need to initialize, AngularJS will initialize the text based on ng-model attribute // Write data to the model function readViewText() { var html = element.html(); // When we clear the content editable the browser leaves a <br> behind // If strip-br attribute is provided then we strip this out if (attrs.stripBr && html === '<br>') { html = ''; } ngModel.$setViewValue(html); } } }; }) </script>
其中參數(shù)類別如下:
部分參數(shù)解釋
restrict:
(字符串)可選參數(shù),指明指令在DOM里面以什么形式被聲明;
取值有:E(元素),A(屬性),C(類),M(注釋),其中默認(rèn)值為A;
E(元素):<directiveName></directiveName>
A(屬性):<div directiveName='expression'></div>
C(類): <div class='directiveName'></div>
M(注釋):<--directive:directiveName expression-->
2.require
字符串代表另一個(gè)指令的名字,它將會(huì)作為link函數(shù)的第四個(gè)參數(shù)
具體用法我們可以舉個(gè)例子說明
假設(shè)現(xiàn)在我們要編寫兩個(gè)指令,兩個(gè)指令中的link鏈接函數(shù)中(link函數(shù)后面會(huì)講)存在有很多重合的方法,
這時(shí)候我們就可以將這些重復(fù)的方法寫在第三個(gè)指令的controller中(上面也講到controller經(jīng)常用來提供指令間的復(fù)用行為)
然后在這兩個(gè)指令中,require這個(gè)擁有controller字段的的指令(第三個(gè)指令),
最后通過link鏈接函數(shù)的第四個(gè)參數(shù)就可以引用這些重合的方法了。
<!doctype html> <html ng-app="myApp"> <head> <script src="http://cdn.staticfile.org/angular.js/1.2.10/angular.min.js"></script> </head> <body> <outer-directive> <inner-directive></inner-directive> <inner-directive2></inner-directive2> </outer-directive> <script> var app = angular.module('myApp', []); app.directive('outerDirective', function() { return { scope: {}, restrict: 'AE', controller: function($scope) { this.say = function(someDirective) { console.log('Got:' + someDirective.message); }; } }; }); app.directive('innerDirective', function() { return { scope: {}, restrict: 'AE', require: '^outerDirective', link: function(scope, elem, attrs, controllerInstance) { scope.message = "Hi,leifeng"; controllerInstance.say(scope); } }; }); app.directive('innerDirective2', function() { return { scope: {}, restrict: 'AE', require: '^outerDirective', link: function(scope, elem, attrs, controllerInstance) { scope.message = "Hi,shushu"; controllerInstance.say(scope); } }; }); </script> </body> </html>
上面例子中的指令innerDirective和指令innerDirective2復(fù)用了定義在指令outerDirective的controller中的方法
也進(jìn)一步說明了,指令中的controller是用來讓不同指令間通信用的。
另外我們可以在require的參數(shù)值加上下面的某個(gè)前綴,這會(huì)改變查找控制器的行為:
(1)沒有前綴,指令會(huì)在自身提供的控制器中進(jìn)行查找,如果找不到任何控制器,則會(huì)拋出一個(gè)error
(2)?如果在當(dāng)前的指令沒有找到所需的控制器,則會(huì)將null傳給link連接函數(shù)的第四個(gè)參數(shù)
(3)^如果在當(dāng)前的指令沒有找到所需的控制器,則會(huì)查找父元素的控制器
(4)?^組合
- AngularJS之自定義服務(wù)詳解(factory、service、provider)
- AngularJS自定義服務(wù)與fliter的混合使用
- Angularjs 自定義服務(wù)的三種方式(推薦)
- AngularJs自定義服務(wù)之實(shí)現(xiàn)簽名和加密
- 詳解AngularJS controller調(diào)用factory
- angularjs封裝$http為factory的方法
- angularJS Provider、factory、service詳解及實(shí)例代碼
- 簡(jiǎn)介AngularJS中使用factory和service的方法
- 詳解AngularJS中自定義過濾器
- AngularJS創(chuàng)建自定義指令的方法詳解
- AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解
相關(guān)文章

淺析Angular 實(shí)現(xiàn)一個(gè)repeat指令的方法

AngularJs1.x自定義指令獨(dú)立作用域的函數(shù)傳入?yún)?shù)方法

深究AngularJS中ng-drag、ng-drop的用法

Angular.JS學(xué)習(xí)之依賴注入$injector詳析

詳解如何構(gòu)建Angular項(xiàng)目目錄結(jié)構(gòu)

angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲(chǔ)的實(shí)例

詳解使用KeyValueDiffers檢測(cè)Angular對(duì)象的變化

使用AngularJS對(duì)表單提交內(nèi)容進(jìn)行驗(yàn)證的操作方法