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

J. *_*son 7 javascript jquery angularjs

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

在我的代码中,我有输入

 <input type="text"  ng-model="c.Id"  ng-init="c.Id={{pId}}"/>
Run Code Online (Sandbox Code Playgroud)

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

 <input type="text"  ng-model="c.Id"  ng-init="c.Id=6"/>
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ine 20

摆脱表达式中的大括号,以便直接从作用域中评估pId

<input type="text"  ng-model="c.Id"  ng-init="c.Id=pId"/>
Run Code Online (Sandbox Code Playgroud)

普拉克


Seb*_*ian 5

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

<input type="text"  ng-model="c.Id"  ng-init="c.Id=pId"/>
Run Code Online (Sandbox Code Playgroud)