angularjs自定義ng-model標簽的屬性
有的時候我們需要為非input類型的元素添加ng-model來實現(xiàn)雙向的數(shù)據(jù)綁定,從而減少冗余代碼,那么可以嘗試一下的方式
例如:我頁面中使用了contenteditable這個屬性來實現(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ù)的,這時就需要為其增加自定義的屬性,如下所示。
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(注釋),其中默認值為A;
E(元素):<directiveName></directiveName>
A(屬性):<div directiveName='expression'></div>
C(類): <div class='directiveName'></div>
M(注釋):<--directive:directiveName expression-->
2.require
字符串代表另一個指令的名字,它將會作為link函數(shù)的第四個參數(shù)
具體用法我們可以舉個例子說明
假設現(xiàn)在我們要編寫兩個指令,兩個指令中的link鏈接函數(shù)中(link函數(shù)后面會講)存在有很多重合的方法,
這時候我們就可以將這些重復的方法寫在第三個指令的controller中(上面也講到controller經(jīng)常用來提供指令間的復用行為)
然后在這兩個指令中,require這個擁有controller字段的的指令(第三個指令),
最后通過link鏈接函數(shù)的第四個參數(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復用了定義在指令outerDirective的controller中的方法
也進一步說明了,指令中的controller是用來讓不同指令間通信用的。
另外我們可以在require的參數(shù)值加上下面的某個前綴,這會改變查找控制器的行為:
(1)沒有前綴,指令會在自身提供的控制器中進行查找,如果找不到任何控制器,則會拋出一個error
(2)?如果在當前的指令沒有找到所需的控制器,則會將null傳給link連接函數(shù)的第四個參數(shù)
(3)^如果在當前的指令沒有找到所需的控制器,則會查找父元素的控制器
(4)?^組合
- AngularJS之自定義服務詳解(factory、service、provider)
- AngularJS自定義服務與fliter的混合使用
- Angularjs 自定義服務的三種方式(推薦)
- AngularJs自定義服務之實現(xiàn)簽名和加密
- 詳解AngularJS controller調(diào)用factory
- angularjs封裝$http為factory的方法
- angularJS Provider、factory、service詳解及實例代碼
- 簡介AngularJS中使用factory和service的方法
- 詳解AngularJS中自定義過濾器
- AngularJS創(chuàng)建自定義指令的方法詳解
- AngularJS基于factory創(chuàng)建自定義服務的方法詳解
相關文章
Angularjs中如何使用filterFilter函數(shù)過濾
這篇文章主要介紹了Angularjs中如何使用filterFilter函數(shù)過濾的相關資料,需要的朋友可以參考下2016-02-02
angular安裝import?echarts?from‘echarts‘標紅報錯解決
這篇文章主要介紹了angular安裝import?echarts?from‘echarts‘標紅報錯解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
AngularJS使用ocLazyLoad實現(xiàn)js延遲加載
這篇文章主要介紹了AngularJS使用ocLazyLoad實現(xiàn)js延遲加載的相關資料,需要的朋友可以參考下2017-07-07
angular json對象push到數(shù)組中的方法
下面小編就為大家分享一篇angular json對象push到數(shù)組中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

