Reputation: 594
Hello I need some help I tried creating a directive which watches the model of an input field if that model changes it should check for special Characters if they are there the model gets parsed and it should return the parsed value.
Now all of that is working fine the value of the input field changed and is correct but it seems only the value changed while the model is still as it was not parsed.
My Html
<input type="text" name="bestellnummer" ng-model="we.bestellnummer" required parse/>
And My Directive
.directive('parse', function ($timeout, Ls, $window) {
return {
restrict: 'AE',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
scope.$watch(attrs.ngModel, function (Val) {
//console.log(Val)
if (Val != undefined) {
if ((Val.toString()).indexOf('?') != -1 && (Val.toString()).indexOf('!') != -1) {
Val = Val.slice(0, -1);
scope.code = new Gs1Parser.Parse(Val);
//console.log(scope.code);
if (scope.code != undefined && scope.code[0].Key != 'invalid') {
var ParsedVal = scope.code[0].Key;
scope.code = undefined;
$window.Gs1Parser.returnedValue = [];
//try with ngModel
ngModel.$modelValue = ParsedVal;
//element[0].value = ParsedVal;
//scope.ab.abNr = ParsedVal;
} else if (scope.code[0].Key == 'invalid') {
Val = Val.slice(1);
//element[0].value = Val;
//scope.ab.abNr = Val;
}
}
}
})
}
}
})
So basically the the user enters ?12345! the Parser changes this to 345 and this should be the new model. At the moment the value is 345 but as soon as I send the Data the modelvale (?12345!) is sent.
Thanks in Advance
Upvotes: 0
Views: 79
Reputation: 9486
Just change scope value, like:
$parse(attrs.ngModel).assign(scope, Val);
(simple scope[attrs.ngModel]=Val
wont work for i.e. 'model.value' only for simple 'value')
You should be careful thow since changing this value will trigger your watch once again.
Upvotes: 1