Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 5和Priming下拉列表动态添加和删除选项_Angular_Typescript_Angular5_Primeng_Primeng Dropdowns - Fatal编程技术网

Angular 5和Priming下拉列表动态添加和删除选项

Angular 5和Priming下拉列表动态添加和删除选项,angular,typescript,angular5,primeng,primeng-dropdowns,Angular,Typescript,Angular5,Primeng,Primeng Dropdowns,我研究角度5和打底。我的项目页面有两个p下拉列表,要求是,如果汽车下拉列表中的标签是“其他”,则在第二个下拉列表中添加一个名为“无油漆”的选项,如果汽车下拉列表标签不是“Ohters”,则从第二个下拉列表中删除“无油漆”选项。我一直在动态地添加和删除下拉选项。请大家指点一下,下面是我的代码。谢谢 Car: <p-dropdown [options]="cars" [(ngModel)]="selectedCar" [filter]="true"></p-dropdown

我研究角度5和打底。我的项目页面有两个p下拉列表,要求是,如果汽车下拉列表中的标签是“其他”,则在第二个下拉列表中添加一个名为“无油漆”的选项,如果汽车下拉列表标签不是“Ohters”,则从第二个下拉列表中删除“无油漆”选项。我一直在动态地添加和删除下拉选项。请大家指点一下,下面是我的代码。谢谢

    Car: <p-dropdown [options]="cars" [(ngModel)]="selectedCar" [filter]="true"></p-dropdown>

    <p-dropdown [options]="paints" [(ngModel)]="selectedPaint" [filter]="true"></p-dropdown>

    constructor() {
            this.cars= [
                {name: 'AA', code: 'aa'},
                {name: 'BB', code: 'bb'},
                {name: 'CC', code: 'cc'},
                {name: 'DD', code: 'dd'},
                {name: 'Others', code: 'others'}
            ];

this.paints= [
                {name: 'XX', code: 'xx'},
                {name: 'YY', code: yyb'},
                {name: 'ZZ', code: 'zz'}
            ];

我确实尝试过这个.cars.pushnew下拉选项'Others','Others',但这是添加'Others'选项的次数与我更改下拉值的次数一样多。

这应该很简单。更改后添加第一个p下拉式车辆事件


我使用ES6完成了以下工作:

if(event.value.code === 'others'){
this.paints.find(paint => paint.code == 'No Paint') === undefined ? this.paints.push({name:'No paint', code: 'No Paint'}) : null;
}
else{
this.paints = this.paints.filter(e => e !== 'Others') //remove 'Others'
}

请创建stackblitzlink@Justcode很抱歉,它在我的组织中被阻止。请尝试任何在线创建者,然后尝试codpen
<p-dropdown [options]="cars" 
(onChange)="checkIfOther($event)" 
[(ngModel)]="selectedCar" 
[filter]="true"></p-dropdown>
checkIfOther(event){
if(event.value.code === 'others'){
this.paints.find(paint => paint.code == 'No Paint') === undefined ? this.paints.push({name:'No paint', code: 'No Paint'}) : null;
}
else{
let idx = this.paints.findIndex(paint => paint.code == 'No Paint';
idx != undefined ? this.paints.slice(idx,1) : null;
}
if(event.value.code === 'others'){
this.paints.find(paint => paint.code == 'No Paint') === undefined ? this.paints.push({name:'No paint', code: 'No Paint'}) : null;
}
else{
this.paints = this.paints.filter(e => e !== 'Others') //remove 'Others'
}