【问题标题】:Angular 5 set select object value manuallyAngular 5手动设置选择对象值
【发布时间】:2018-07-22 17:57:33
【问题描述】:

我有一个选择,我想手动设置一个对象值。我尝试将 setValue 或 patchValue 与 Object 一起使用,但它不起作用。

<mat-select placeholder="Selecione a resposta" (change)="onSelectFraudResponse($event.value)" formControlName="response">
  <mat-option *ngFor="let res of fraudResponses" [value]="res">
    {{res.name}}
  </mat-option></mat-select>

我知道同时使用模板表单和响应式表单是不正确的。

【问题讨论】:

    标签: angular forms select setvalue reactive-forms


    【解决方案1】:

    对于绑定对象,您需要使用[ngValue],因为[value] 用于原始类型

    编辑:

    由于我没有看到具体示例如何在mat-select 中双向绑定对象,我建议您使用一个属性来识别您的对象,而不是更改整个对象,您可以更改该值,双向数据绑定的正确属性是[(value)]

    【讨论】:

    • 知道了!但是当我设置 [ngValue] 时无法绑定到“ngValue”,因为它不是“mat-select”的已知属性。还是我设置不正确?抱歉,感谢您的帮助!
    • 似乎组件 mat-select 不提供对该属性的访问权限,从他们的文档看来,在我看来,您只能使用双向数据绑定将原始值绑定到 mat-select,所以您不能将object 传递给[value] 而是一个字符串,我现在更新我的答案
    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多