我有这个
<select id="categoryListBox"
data-ng-model="list"
data-ng-change="updateCategory(item)"
data-ng-options="item as item.categoryId[0].name+' : '+item.name for item in list"
class='form-control' >
<option value="" style="display:none;"> current category </option>
</select>
每当updateCategory
被点击时,我console.log
参数,这应该是我选择的项目。 console.log
将其读为undefined
。
我还console.log
模型$scope.list
,并将其记录为由我选择的item
替换。
发生了什么?
这是updateCategory
功能
$scope.updateCategory = function(item){
console.log('updateCategory hit');
console.log('list is now',$scope.list)
console.log(item);
};
这是
updateCategory hit
list is now [what item should be]
undefined
html包含在ng-hide
内,如果重要的话
答案 0 :(得分:0)
您的ng-change
与ng-options
的范围不同。从理论上讲,你基本上只需要传入模型,因为它会在ng-change
之前更新:
updateCategory(list)
但是如您所见,因为它是您的模型和您的ng选项,您可以覆盖该列表。所以你需要做的就是让你的模型成为别的东西:
<select id="categoryListBox"
data-ng-model="selected_item"
data-ng-change="updateCategory(selected_item)"
>
所以 - 回顾一下,模型将被设置为所选项目的任何内容。如果将模型设置为与列表相同,则列表将成为您选择时所选项目的任何内容。可能不是你想要的。因此,将模型设置为与列表不同的变量,然后您可以将模型传递给ng-change函数。