我在页面上有多个选择框,每个选择框都有一个列出其价格的范围。在选择框上,根据项目动态更改价格更新。我如何使用angularjs实现这一目标?
我的代码
<div ng-repeat="items in itemsList" class="col-md-3 col-sm-3 ">
<div class="product product-category">
<div class="details">
<img src="resources/images/itemImages/{{items.image}}" style="width:159;height:159;" class="image" />
<h4 class="name">{{items.name}}<span class="quality">(Quality)</span></h4>
<h4 class="info">{{items.description}}</h4>
<select id="unit{{items.id}}" ng-init="itemUnitsSelected = items.itemUnitPrices[0]" ng-model="itemUnitsSelected" ng-change="changedValue(itemUnitsSelected, '{{items.id}}')" data-ng-options="XYZ as XYZ.unit.name for XYZ in items.itemUnitPrices track by XYZ.id " class="form-control item-count">
</select>
</div>
<div class="cart-feature">
<h5 class="price">
<span class="rate original"></span>
<span class="rate discounted" price="{{items.itemUnitPrices[0].itemPrice}}" id="discountedAmount{{items.id}}">Rs. {{items.itemUnitPrices[0].itemPrice}}</span>
</h5>
<div class="cart-option">
<span class="tag">Qty </span>
<span class="quantity">
<input type="text" value="1" id="qty{{items.id}}" class="form-control" />
</span>
<a href="javascript:return;" class="event" ng-click="addItemToCart(items.id,items.name)">
<span class="cart-btn"></span>
</a>
</div>
</div>
</div>
</div>
在我的控制器上目前我正在通过javascript更新值,我想更改并使用angularjs绑定它。
$scope.changedValue=function(item,itemId){
document.getElementById("discountedAmount"+itemId).innerHTML = "Rs. "+item.itemPrice;
document.getElementById("discountedAmount"+itemId).setAttribute("price", item.itemPrice);
};
答案 0 :(得分:3)
itemUnitsSelected
转换为ng-repeat
,{p> ng-model
应与item.itemUnitsSelected
的每个项目绑定。您使用的ng-change
指令不应该{{}}
在其中插入
<select id="unit{{items.id}}"
ng-init="item.itemUnitsSelected = items.itemUnitPrices[0]"
ng-model="itemUnitsSelected"
ng-change="changedValue(itemUnitsSelected, item.id)"
data-ng-options="XYZ as XYZ.unit.name for XYZ in items.itemUnitPrices track by XYZ.id "
class="form-control item-count">
</select>
不要从控制器进行DOM操作,它被认为是BAD 实践。
您还可以摆脱ng-change事件,只需更正span标记,在span标记中将items
重命名为item
。
<span class="rate discounted"
price="{{item.itemUnitPrices[0].itemPrice}}"
id="discountedAmount{{item.id}}">
Rs. {{item.itemUnitPrices[0].itemPrice}}
</span>