[英]Updating one value in angular model according to another input
假設我有一個角度模型,我的模型中有三個變量,比如a,b,c
,我想要c=a*b
。
我通過添加一個更新c
的函數updateC()
來完成它,然后我的HTML看起來像這樣:
<input name="a" ng-model="a" ng-change="updateC()"></input>
<input name="b" ng-model="b" ng-change="updateC()"></input>
<input name="c" ng-model="c" readonly></input>
但是如果我有很多這樣的三元組,這不是一個好的解決方案,因為我需要為每個這樣的三元組定義一個函數。 有沒有辦法將模型中元素的值綁定到某個公式? 即有一些屬性ng-like-bind-but-updates-model
,以便以下html可以工作:
<input name="a" ng-model="a"></input>
<input name="b" ng-model="b"></input>
<input name="c" ng-model="c" ng-like-bind-but-updates-model="a*b" readonly></input>
這似乎有效,不確定這是否是你想要實現的:
<input name="a" ng-model="a"></input>
<input name="b" ng-model="b"></input>
<input name="c" value="{{c=a*b}}" readonly></input>
你可以簡單地綁定輸入C的值
<input name="c" value="{{c=a*b}}"></input>
<label>{{c}}</label>
像這樣: http : //codepen.io/Visualife/pen/xwNKjj
對於a&b,你需要處理初始值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.