Angularjs使用ng-init为ng-model分配值

时间:2014-04-25 22:51:40

标签: javascript jquery angularjs

嗨我有以下问题,这似乎很容易,应该工作,但不是。

在我的代码中,我输入了

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

当我使用firebug工具查看DOM时,我看到了值

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

但它不会在输入框中显示6,也不能使用#scope访问它。 请告诉我这里有什么问题以及如何修复它以便ng-model可以来自ng-init。 谢谢

3 个答案:

答案 0 :(得分:20)

摆脱表达式中的大括号,以便直接从范围

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

Plunk

答案 1 :(得分:5)

ng-init采用表达式,因此您不需要大括号:

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

答案 2 :(得分:2)

使用ng-value将值分配给ng-model

   <input type="text" class="form-control" ng-model="inputPrice" />
   <input type="text" ng-model="newPrice" ng-value="newPrice=inputPrice" />
相关问题