Artem Z
Artem Z

Reputation: 565

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?

Upvotes: 4

Views: 203

Answers (1)

Jorawar Singh
Jorawar Singh

Reputation: 7621

You are not setting any value to scope in order to angular can watch and react on that, your 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, '');
}

Upvotes: 6

Related Questions