[英]Getting return Value [object Object] for dropdown at first result. It changes when i select an option and works. How to fix the default result
I have an angular 7 application. 我有一个角度7应用程序。 I have called services to fetch mysql data.
我已经调用了服务来获取mysql数据。 In a form I want to populate a dropdown and want to validate it.
在表单中,我想填充下拉列表并想要验证它。 But the dropdown always has an undefined value at first so i cannot validate it with required.
但是下拉列表一开始总是有一个未定义的值,所以我不能用必需的值验证它。 Why does it have an [object Object] at first?
为什么它最初有[对象]? But it works when i properly select a value.
但是当我正确选择一个值时,它就有效。
I have used it in a component and works great using the code below 我已经在组件中使用它,并使用下面的代码很好地工作
Component.ts file
this.employeeservice.retrieveAllTodos().subscribe(
response => {
this.employeesList = response;
console.log("it works");
}
);
<select class="custom-select custom-select-sm" [class.ng-invalid]="!ecode.value" name="ecode" #ecode="ngModel" required [ngModel]="ecode" (ngModelChange)="getId($event,ecode.value)">
<option *ngFor="let todo of employeesList" value={{todo.employeeCode}}>{{todo.employeeCode}} - {{todo.employeeName}}</option>
</select>
<div *ngIf="ecode.errors && (ecode.dirty || ecode.touched || todoForm1.submitted)">
<div *ngIf="ecode.errors.required">
<small class="form-text text-muted">Please select code</small>
</div>
<div *ngIf="ecode.invalid">
<small class="form-text text-muted">Please select code1</small>
</div>
</div>
<pre> {{ecode.value}}</pre>
<pre *ngIf="ecode.value === null">no value</pre>
https://imgur.com/OjSF2Wv https://imgur.com/JY7zhYD https://imgur.com/OjSF2Wv https://imgur.com/JY7zhYD
I have included 2 pictures of my situation 我已经包含了2张我的情况照片
The issue is with this "[ngModel]="ecode""
looks like ecode is an object 问题是这个
"[ngModel]="ecode""
看起来像ecode是一个对象
Change this to "[ngModel]="ecode.value""
.It should work 将其更改为
"[ngModel]="ecode.value""
它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.