簡體   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