I want to encapsulate a inputBox into AngularJS component. This component will automatically add some prefix to the input before it's passed to binding model data. For example the prefix is "testPrefix", when user input "ABC", the corresponding model data will be "testPrefixABC".
My code is like this:
angular.module('').component('dummyBox', {
bindings: {
ngModel: '='
},
require: {
ngModelCtrl: 'ngModel'
},
template: '<span><input type="text" ng-model="$ctrl.ngModel" /></span>',
controller: function() {
$ctrl.$onInit = () => {
$ctrl.ngModelCtrl.$parsers.push((viewValue) => {
if(viewValue) return "testPrefix" + viewValue;
});
$ctrl.ngModelCtrl.$formatters.push((modelValue) => {
return modelValue.substr("textPrefix".length);
});
}
}
});
<dummy-box ng-model="outScopeVar"></dummy-box>
<label>{{outScopeVar}}</label>
For now it's not working, the content in label is still the input value, no prefix string added. Any help will be appreciated.
Thanks in advance.