如何截获价值吴模型指令结合 [英] How to intercept value binding by ng-model directive
本文介绍了如何截获价值吴模型指令结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,
如果我有一个范围值:
$scope.bankInfo = '808,CityBank';
和输入字段:
<input name="bank_number" ng-model="bankInfo " />
我不知道什么是我的输入字段只绑定到数字部分(即808)的正确方法。
我应该使用自定义的指令?
I don't know what is the proper way to bind my input field only to digit part (that is 808). Should I use custom directive?
推荐答案
我发现,定制指令是拦截约束力的一个好方法。
让申报指令是这样的:
I found that custom directive is a good approach to intercept binding. Let declare a directive like this:
module.directive('banknumber', function () {
return {
require: 'ngModel',
link: function ($scope, $element, $attrs, modelCtrl) {
modelCtrl.$formatters.push(function (inputValue) {
var modelValue = modelCtrl.$modelValue;
var bkno = (modelValue == null) ? '' : modelValue.split(',')[0];
return bkno;
});
modelCtrl.$parsers.push(function (inputValue) {
var modelValue = modelCtrl.$modelValue;
var bankName = (modelValue == null) ? '' : modelValue.split(',')[1];
return modelCtrl.$viewValue + ',' + bankName;
});
}
};
});
适用于输入和它的完成:
apply to input and it's done:
<input name="bank_number" ng-model="bankInfo" banknumber/>
这篇关于如何截获价值吴模型指令结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文