8

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?

5
  • you could take use of ng-init directive simply Commented Jul 2, 2015 at 21:18
  • or see this answer stackoverflow.com/a/30471421/3711660 - it will work even if user enters text, and deletes it - default value will be inserted Commented Jul 2, 2015 at 21:19
  • @pankajparkar Do you mean something like this <input ng-init="default-value" ng-model="final-value"> where the value entered by the user will be accessible as final-value? Commented Jul 2, 2015 at 21:36
  • @NipunParasrampuria what do you mean by this? Commented Jul 2, 2015 at 21:40
  • Lets say the web page's HTML contains an element like the following: <input ng-init="default-value" ng-model="final-value">, 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? Commented Jul 2, 2015 at 21:48

1 Answer 1

8

Set the ngModel value:

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

$scope.myInput = "Default";
Sign up to request clarification or add additional context in comments.

2 Comments

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
@NipunParasrampuria -- Yeah, $scope.myInput will reflect whatever you put in that input

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.