7

How do I set the default value of an input textbox in AngularJS so that it can be altered later? I wish to be able to submit the changed value of the textbox(using ng-model) to the server. Would using ng-value to set the initial value of the textbox be the correct approach in this case?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • you could take use of `ng-init` directive simply – Pankaj Parkar Jul 02 '15 at 21:18
  • or see this answer http://stackoverflow.com/a/30471421/3711660 - it will work even if user enters text, and deletes it - default value will be inserted – Toumash Jul 02 '15 at 21:19
  • @pankajparkar Do you mean something like this where the value entered by the user will be accessible as final-value? – Nipun Parasrampuria Jul 02 '15 at 21:36
  • @NipunParasrampuria what do you mean by this? – Pankaj Parkar Jul 02 '15 at 21:40
  • Lets say the web page's HTML contains an element like the following: , where default-value is the value that I wish the textbox contain when the page is loaded. If the user later alters the textbox so that it contains a value other than the default-value, would I be able to access that value using $scope.final-value? – Nipun Parasrampuria Jul 02 '15 at 21:48

1 Answers1

8

Set the ngModel value:

<input type="text" ng-model="myInput" />

$scope.myInput = "Default";
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • Thanks for the prompt reply :). I had thought of that, but would I be able to access the updated value of that field using $scope.myInput when I wish to submit data to the server – Nipun Parasrampuria Jul 02 '15 at 21:28
  • @NipunParasrampuria -- Yeah, `$scope.myInput` will reflect whatever you put in that `input` – tymeJV Jul 02 '15 at 21:36