【发布时间】:2017-07-18 08:46:04
【问题描述】:
我需要格式化输入值,因此我创建了一个指令,该指令使用带有 require: 'ngModel' 的模板,因为我必须使用 ngModelController 函数( $parsers、$formatters 等)。
这是我的 HTML:
<div ng-model="myInputValue" amount-input-currency=""></div>
{{myInputValue}}
这是我的指令:
.directive('amountInputCurrency', [function(){
return {
templateUrl: '../amountInputCurrency.tmpl.html',
require: 'ngModel',
restrict: 'A',
link: function(scope, elem, attrs, model) {
// ...
}
}
}
这是我的模板:
<input type="text" ng-model="myInputValue">
问题是在格式化插入的值后我无法更新视图。例如,如果我写 '1' 我想以这种方式更改值:
model.$formatters.push(function(value) {
return value + '00';
}
我尝试以其他方式设置事件:
<input type="text" ng-model="myInputValue" ng-blur="onBlur()">
scope.onBlur = function() {
model.$viewValue = model.$viewValue + '00';
// or model.$setViewValue(model.$viewValue + '00';);
model.$render();
};
model.$viewValue 发生变化,myInputValue(在带有 {{myInputValue}} 的 HTML 中)发生变化,但 不是输入框中显示的值...这是什么问题?谢谢!
----------------更新----------------
问题可能是因为我有 2 个 ng-model(一个在 HTML 中,一个在模板中):https://github.com/angular/angular.js/issues/9296
我该怎么办?两个模型都指同一个模型...
【问题讨论】:
标签: angularjs input formatting angular-ngmodel angular-template