If I use
<input type="text" value="asdf">
Then I see an input with the default value of asdf
but if I do
<input data-ng-model="modelname" type="text" value="asdf">
Then I can use the modelname
in the controller but the default value no longer shows up. How can I use both things?
Tools:
Typically you would set the default value of modelname inside of a controller. It is possible to cheat a little and use the 'ngInit' directive, though the angular documentation discourages its use outside of ngRepeat.
<input data-ng-model="modelname" data-ng-init="modelname = 'asdf'" type="text">
Relevant statement from angular docs
"The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With