AngularJS view does not update when value is replaced

I have the following html:

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

function:

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

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

Now the .log console shows that the line has successfully changed, however, the view is not updated. The strangest part is when using the view updates substring!

string = string.substring(0, 10);

What's wrong?

+4
source share
1 answer

You do not set any value to scope so that angular can observe and respond to it, your function does something, but never sets a value for the scope.

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

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

Source: https://habr.com/ru/post/1650523/


All Articles