繁体   English   中英

AngularJS使用ng-init将值分配给ng-model

[英]Angularjs assigning value to ng-model using ng-init

嗨,我有以下问题似乎很简单,应该可以,但并非如此。

在我的代码中,我输入了

 <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-init可以使用ng-model。 谢谢

删除表达式中的花括号,以便它将直接从范围中计算pId

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

朋克

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM