好吧,所以我正在为假货建造一个假的盘子制造商。 我有一个表格,其中包含构建该表格所需的一切,而我唯一遇到的麻烦是成分表。 这是我的HTML:
<div class="icheckbox" ng-repeat="ing in ingredientList">
{{ing}}
<input type="checkbox" name="{{ing}}" ng-click="addIngredient(ing)"/>
<input type="number" name="more" ng-model="ings.ing.more" placeholder="price for extra"/>
<input type="number" name="less" ng-model="ings.ing.less" placeholder="price for less/none"/>
</div>
这是(相关的)JS:
$scope.ings = {};
$scope.ingredientList = ['lettuce', 'tomatoe', 'steak', 'pulled pork', 'green onion', 'red onion', 'mozarella', 'cheddar', 'bacon bits', 'chicken'];
$scope.addIngredient = function(which){
$scope.ings[which] = which;
}
$scope.makeItem = function(item){
item.ingredients = $scope.ings;
console.log(item);
ItemService.createItem(item)
.then(handleRes, handleRes);
}
正如您确定的那样,当我尝试在复选框中查找特定成分并为该特定成分或多或少添加价格修饰符时,它会修改列表中所有成分的价格。 例如,由于模型的原因,我不能有单独的修饰符。 我知道这是模型的问题,但是如何重写代码以完成我的工作呢?
对于那些在选择了正确答案后阅读此书的人,请参见此处的小提琴,以查看我所使用的内容的更准确的HTML。 顺便说一句,小提琴无法完全正常工作(出于我不知道的原因,但我很少使用JSFiddle),但是如果您看到我认为的内容,那么很容易理解我正在尝试做的事情。 http://jsfiddle.net/Lrpjwyrg/13/