我寫了兩條指令。其中之一是用於與單位輸入數字(如「1M」):在同一型號上發佈兩條指令
angular.module('ng', [])
.directive('unit', function() {
return {
require: 'ngModel',
link: function (s, e, attributes, ngModel) {
ngModel.$parsers.push(function (i) {
return i + attributes.unit;
});
ngModel.$formatters.push(function (i) {
return (i + '').replace(attributes.unit, '');
});
}
}
});
第二個取代,
與.
,因爲很多人在歐洲使用逗號而不是小數點的小數,我想我的值被標準化。
angular.module('ng', [])
.directive('numberWithPoint', function() {
return {
require: 'ngModel',
link: function (s, e, attributes, ngModel) {
ngModel.$parsers.push(function (i) {
return i.replace(',', '.');
});
}
}
});
的用法是:
<input type="text" ng-model="howLongSomethingIs" unit="m" number-with-point /> m
問題:如果是獨自一人,如果我添加了number-with-point
指令後,unit
將不起作用unit
的偉大工程的指令(所顯示的數值是示例1m
不是1
。
我試圖搞砸return
對象內的priority
屬性,但沒有發生任何事情。
如何使這兩個指令一起工作?
在這jsfiddle,它似乎工作,但它使用Angular 1.2.1。我正在使用Angular 1.3.14。有人知道如何讓jsfiddle使用其他版本?
你可以連接兩個指令的plunker/jsbin嗎?這肯定有助於調試它。 –
好的,好主意。稍等片刻 –
請參閱編輯。我添加了一些意見 –