Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs assigning value to ng-model using ng-init

Hi I have following issue which seems easy and should work but is not.

In my code I have input

 <input type="text"  ng-model="c.Id"  ng-init="c.Id={{pId}}"/>

When I look at the DOM using firebug tool I see the value

 <input type="text"  ng-model="c.Id"  ng-init="c.Id=6"/>

But it wont display 6 in the input box neither I can access it using #scope. Please let me know what is wrong here and how to fix it so that ng-model can have from ng-init. Thanks

like image 601
J. Davidson Avatar asked Apr 25 '14 22:04

J. Davidson


3 Answers

Get rid of the braces in the expression so that it will evaluate pId directly from the scope

<input type="text"  ng-model="c.Id"  ng-init="c.Id=pId"/>

Plunk

like image 150
Marc Kline Avatar answered Oct 14 '22 23:10

Marc Kline


ng-inittakes an expression and therefore you do not need the curly braces:

<input type="text"  ng-model="c.Id"  ng-init="c.Id=pId"/>
like image 22
Sebastian Avatar answered Oct 15 '22 00:10

Sebastian


Assigning the value to ng-model using ng-value

   <input type="text" class="form-control" ng-model="inputPrice" />
   <input type="text" ng-model="newPrice" ng-value="newPrice=inputPrice" />
like image 2
raviook Avatar answered Oct 15 '22 00:10

raviook