40

I have input like this

<input type="text" name="widget.title" ng-model="widget.title" value="{{widget.title}}"/>

I want to change input value dynamically so i use that but it doesn't change the value:

$scope.widget.title = 'a';
mTuran
  • 1,846
  • 4
  • 32
  • 58

5 Answers5

68

You don't need to set the value at all. ng-model takes care of it all:

  • set the input value from the model
  • update the model value when you change the input
  • update the input value when you change the model from js

Here's the fiddle for this: http://jsfiddle.net/terebentina/9mFpp/

Dan Caragea
  • 1,784
  • 1
  • 19
  • 24
17

If you don't wan't to use ng-model there is ng-value you can try.

Here's the fiddle for this: http://jsfiddle.net/Rg9sG/1/

Frank6
  • 1,193
  • 1
  • 11
  • 23
  • 1
    On the fiddle anyway, this seems to only work if the value in the input field has never been changed. – vossad01 Aug 26 '14 at 13:14
  • 1
    I have the same problem, I cannot change the value from the controller – Paranoid Android Sep 22 '14 at 13:20
  • Only works for setting the value, but won't update the scope variable after the user changed the input: "It can also be used to achieve one-way binding" (https://docs.angularjs.org/api/ng/directive/ngValue) – JimmyBlu Aug 16 '16 at 16:13
  • This was an awesome solution for my datepickers. – WiseGuy Sep 27 '16 at 15:46
2

Use ng-value for set value of input box after clicking on a button:

"input type="email"  class="form-control" id="email2" ng-value="myForm.email2" placeholder="Email"

and

Set Value as:

 $scope.myForm.email2 = $scope.names[0].success;
atiquratik
  • 1,296
  • 3
  • 27
  • 34
-1

Some times there are problems with funtion/features that do not interact with the DOM

try to change the value sharply and then assign the $scope

document.getElementById ("textWidget") value = "<NewVal>";
$scope.widget.title = "<NewVal>";
Danny Mora
  • 11
  • 3
-6

{{widget.title}} Try this it will work

Hablu
  • 203
  • 1
  • 4
  • 10