我的HTML页面上有一个列表。该列表使用ng-options从数组中抽取。对象数组的属性之一是show。 ng-options使用一个过滤器来只显示show == 1的选项。当用户点击一个按钮时,列表中的选择项被添加到一个数组,并且show值需要被设置为0,以便它从名单。该值被设置为0,但ng选项现在正确地打印选项的选项。更新值时更改为字符串数组
数组定义
referenceTypes: Array<any> = [
{ label: 'Customer Reference', show: 1 },
{ label: 'PO Number', show: 1 },
{ label: 'SO Number', show: 1 }
]
按钮单击
addReferenceType =() => {
...
// hide the reference type from the add drop down list
this.referenceTypes.filter(option => option.label == this.referenceType)[0].show = 0;
}
HTML
<select ng-model='$ctrl.referenceType'
ng-options="option.label as option.label for option in $ctrl.referenceTypes | filter: {show: 1}">
<option value=""></option>
</select>
嗨,我试过你的代码,它似乎正在为我工作正常。 您能否创建一个工作示例,以便我们可以重现该问题。请检查以下链接 [在jsfiddle上尝试过的示例](https://jsfiddle.net/25z9ck4n/2) –
@KRISHNAPATEL您创建的jsfiddle在单击select时出现以下错误:'Can not set property'show'of undefined' – quirimmo
@KRISHNAPATEL你需要以这种方式改变你的代码,否则有一次'filter'返回一个空数组,并且你有错误,因为'click'被触发两次。 'addReferenceType(){ let filteredTypes:Array = this.referenceTypes.filter(option => option.label == this.referenceType);如果(filteredTypes.length> 0){ filteredTypes [0] .show = 0; } }' –
quirimmo