Angular 在下拉列表中显示列表
我想使用Angular在下拉列表中显示列表。我试图实现这一点: 打字稿: 建造师Angular 在下拉列表中显示列表,angular,typescript,angular6,Angular,Typescript,Angular6,我想使用Angular在下拉列表中显示列表。我试图实现这一点: 打字稿: 建造师 export class Merchant { constructor( public id: string, public name: string ) {} } 组成部分: import {Merchant} from "../domain/merchant"; import {MerchantService} from "../service/merchant.service";
export class Merchant {
constructor(
public id: string,
public name: string
) {}
}
组成部分:
import {Merchant} from "../domain/merchant";
import {MerchantService} from "../service/merchant.service";
@Component({
selector: 'app-terminal',
templateUrl: './terminal.component.html',
styleUrls: ['./terminal.component.scss']
})
export class TerminalComponent implements OnInit {
. ..........
constructor(private terminalService: TerminalService,
private merchantService: MerchantService,
private router: Router,
private route: ActivatedRoute) {
}
merchants: Merchant[];
ngOnInit() {
this.route.params.pipe(
flatMap(params => {
if (params['id']) {
return this.terminalService.get(params['id']);
} else {
return of(null);
}
})
......
}
}
HTML代码:
<div class="form-group type">
<div class="input-group-prepend">
<label for="type">Merchant</label>
</div>
<select class="custom-select" name="type" [(ngModel)]="terminal" id="type" required>
<option selected></option>
<option [value]="type" *ngFor="let merchant of merchants">{{merchant.name}}</option>
</select>
</div>
我的下拉列表是空的。你知道我错在哪里吗?你在空的商人数组上迭代选项,而且它也没有初始化 您的选择中没有项目。例如,请尝试:
merchants: Array<Merchant> = [{name:'test' , id:'test'}];
你在商家阵列中有任何价值吗?我如何检查?你在哪里添加商家阵列中的项目?对不起,但我是这项技术的新手?@SunilSingh这是唯一的代码。此外,我还可以展示服务: