4

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?

3
  • No idea but -- this is a bit unorthodox -- add a last line to your function that says $scope.$apply();. Commented Aug 8, 2016 at 9:40
  • $scope.$apply() needs when you are working on third parties libraries. Commented Aug 8, 2016 at 9:42
  • replace in your checkInput function var string = $scope[value] by var string =value; and then try it Commented Aug 8, 2016 at 9:45

1 Answer 1

6

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, '');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Jorawar Singh, thank you, that's exactly what I needed. What about substring? Why does it work?
you are replacing the sting like string = string.substring(0, 10); and that's correct but you were not setting it to scope. var i = 10; 10 = 5; now i is 5. you have changed the original value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.