Default value of input textbox in AngularJS
Asked Answered
D

1

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?

Doreendorelia answered 2/7, 2015 at 21:16 Comment(5)
you could take use of ng-init directive simplyHark
or see this answer https://mcmap.net/q/48591/-angularjs-template-default-value-if-binding-null-undefined-with-filter - it will work even if user enters text, and deletes it - default value will be insertedManipur
@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?Doreendorelia
@NipunParasrampuria what do you mean by this?Hark
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?Doreendorelia
M
8

Set the ngModel value:

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

$scope.myInput = "Default";
Moonstruck answered 2/7, 2015 at 21:18 Comment(2)
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 serverDoreendorelia
@NipunParasrampuria -- Yeah, $scope.myInput will reflect whatever you put in that inputMoonstruck

© 2022 - 2024 — McMap. All rights reserved.