Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS input ng-model / default value conflict

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:

  • ionic
  • AngularJS
like image 280
Jacksonkr Avatar asked Mar 17 '23 09:03

Jacksonkr


1 Answers

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."

like image 133
Scott Vickers Avatar answered Apr 02 '23 02:04

Scott Vickers