-1

I have an HTML input that is assigned to an ng-model value, before any manual change in input, all the changes of ng-model in the controller (pragmatically) are displayed in HTML input but if I write anything in input manually, no change in controller be effected on displayed input on-page.

I write $scope.$apply() but it is not worked. also changing vieweValue of input by calling $setVieweValue doesn't have any effect on input element value. when I debug, I find out that the reason is that the DOM value of the element is unchangeable (changing ng-model value doesn't change input value) because when I change the value of the DOM element of input in chrome console, then the value becomes displayed in the page.

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 1
    You are supposed to share some of your tried code. – rahim.nagori Feb 17 '20 at 07:25
  • New AngularJS developers often do not realize that `ng-repeat`, `ng-switch`, `ng-view`, `ng-include` and `ng-if` all create new child scopes, so the [data hiding] problem often shows up when these directives are involved. This issue with primitives can be easily avoided by following the "best practice" of [always have a '.' in your ng-models](http://www.youtube.com/watch?v=ZhfUv0spHCY&feature=youtu.be&t=30m) – watch 3 minutes worth. Misko demonstrates the primitive binding issue with ng-switch. – georgeawg Feb 17 '20 at 13:48

1 Answers1

0

Try putting your model inside an object. eg- Suppose your scope variable is:

$scope.inputValue = "someValue";

and you are binding it as:

ng-model="inputValue";

Change it like this:

$scope.ob = {inputValue: "someValue"};

and in html:

ng-model="ob.inputValue";
Akash Bhardwaj
  • 346
  • 1
  • 3
  • 9
  • Thanks a lot !! it solve my problem, it was wasting my time for 2 days, but it is so strange for me, I cannot understand why an object solve this problem. can you explane the reason? – Davood Mojahedi Feb 17 '20 at 07:37
  • Most probably you are using any one of ng-repeat, ng-switch, ng-view, ng-include and ng-if. These all create new child scopes and hence your input is unable to detect the ng-model in this scope. When you use the object, prototypical inheritance comes into play and the variable bound to ng-model is found in the parent scope. – Akash Bhardwaj Feb 17 '20 at 08:57