简体   繁体   English

获取返回值[object Object]以获取第一个结果的下拉列表。 当我选择一个选项并且有效时它会改变。 如何修复默认结果

[英]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.

相关问题 单击角度选择选项下拉菜单中的按钮时如何选择对象的多个值 - How to select multitple value of object when click on button in Angular selection option dropdown menu 对象生成器函数未执行应做的操作,并且不返回任何结果。 - Object generator function not doing what it should and returning no result. 输出结果时获取[object Object] - Getting [object Object] when outputting result 在Java中检查结果时如何修复“未定义”对象? - How to fix 'undefined' object when checking result in Javascript? 默认值不是? 对象:null吗? 在选择选项Angularjs中 - The default value is not ? object:null ? in select option Angularjs Select 下拉选项值在更改之前不会受到影响 - Select dropdown option value not getting affected until it changes 如何选择第一/默认 <option>模态值 - How to select first/default <option> value in Modal 选中第一个选择框后,根据第一个选择结果显示第二个结果。 但是在POST期间没有任何结果 - After first selection box selected, display second result based on first selection result. But not getting any result while POST 访问对象上具有默认值的字段时如何解决流错误? - How to fix Flow error when accessing a field with default value on an object? 我需要了解以下代码是如何工作的。 从该阵列到 object 的步骤是什么 - I need to understand how the following code works. What is the step by step to get from that array to the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM