Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

PS: This is my first question on StackOverflow, so please be generous in case there are any ambiguities in my question :)

share|improve this question
    
you could take use of ng-init directive simply – Pankaj Parkar Jul 2 '15 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 – Toumash Jul 2 '15 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? – Nipun Parasrampuria Jul 2 '15 at 21:36
    
@NipunParasrampuria what do you mean by this? – Pankaj Parkar Jul 2 '15 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? – Nipun Parasrampuria Jul 2 '15 at 21:48
up vote 6 down vote accepted

Set the ngModel value:

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

$scope.myInput = "Default";
share|improve this answer
    
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 2 '15 at 21:28
    
@NipunParasrampuria -- Yeah, $scope.myInput will reflect whatever you put in that input – tymeJV Jul 2 '15 at 21:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.