Artem Z Artem Z - 4 months ago 9
Javascript Question

AngularJS view doesn't update when replacing value

I have the following html:

<input type="text" ng-model="skype" ng-change="checkInput('skype')">


the function is:

$scope.checkInput = function(value) {
var string = $scope[value],
pattern = /[^a-zA-Z]+/g;

string = string.replace(pattern, '');
}


Now the console.log shows, that the string replaced successfully, however the view doesn't update.
The strangest part is when using substring the view updates!

string = string.substring(0, 10);


What's wrong?

Answer

You are not setting any value to scope in order to angular can watch and react on that, you function is doing something but never setting value to scope.

$scope.checkInput = function(value) {
    var string = $scope[value],
        pattern = /[^a-zA-Z]+/g;

    $scope.skype = string.replace(pattern, '');
}