[英]Selected option Angular 4, ngValue don't work
Im trying to have a selected option when I receive the object workOrder.supplier, I tryed NgValue, ng-selected, compareWith but dont work, I dont know Why... this project use Angular 4. You have another option to I try?当我收到 object workOrder.supplier 时,我试图选择一个选项,我尝试了 NgValue、ng-selected、compareWith 但没有用,我不知道为什么...这个项目使用 Angular 4. 你有另一个选项让我试试吗?
<md-select class="suppliers-list" placeholder="Selecione Sua Empresa" [(ngModel)]="workOrder.supplier" formControlName="supplier" name="supplier" [disabled]="schedulingDisabled" >
<md-option *ngFor="let supplier of suppliers" [value]="supplier" >
{{ supplier.corporateName }}
</md-option>
</md-select>
Edited:编辑:
Try this.. this worked for me.试试这个..这对我有用。 Your question is bit unclear of what you are trying to do.
您的问题不清楚您要做什么。 In order to clarify remove the disabled property and see.
为了澄清删除禁用的属性并查看。
<md-select formControlName="supplier" [(ngModel)]="workOrder.supplier">
<md-option *ngFor="let supplier of suppliers" [value]="supplier">{{supplier.displayName}} </md-option>
</md-select>
edited: The TS that get the info已编辑:获取信息的 TS
newWorkOrder() {
let newWorkOrder = new WorkOrder();
// se verdadeiro, pega os dados que vieram do Plano de manutenção ou do editar
if (this.workOrderService.getOSCache() !== null && this.workOrderService.getOSCache() !== undefined) {
this.workOrderService.getOSCache().subscribe(res => {
newWorkOrder = res;
this.selectedSupplier = res.supplier;
if (res.initialDate != null) {
this.selectedDateInitial = new Date(res.initialDate);
}
if (res.finalDate != null) {
this.selectedDateFinal = new Date(res.finalDate);
}
this.workOrderService.clearOSCache();
});
} else {
newWorkOrder.initializeWithJSON(this.createForm());
}
return newWorkOrder;
} }
Im still not clear of what you are asking.. I Assume that you want to capture the value in the ts file in selected input in template file.我仍然不清楚你在问什么。我假设你想在模板文件中的选定输入中捕获 ts 文件中的值。
<md-select formControlName="supplier" [(ngModel)]="workOrder.supplier" (change)="newWorkOrder(selectedSupplier.value)" #selectedSupplier>
<md-option *ngFor="let supplier of suppliers" [value]="supplier">{{supplier.displayName}} </md-option>
</md-select>
And in your ts file:在你的 ts 文件中:
newWorkOrder(selectedValue:any) {
console.log(selectedValue);
}
In this solution I have used something called angular template refferencing.在此解决方案中,我使用了称为 angular 模板引用的东西。 You can read more about it in the official angular documentation here
您可以在此处的官方 angular 文档中阅读更多相关信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.