繁体   English   中英

如何将多个动作绑定到单个 Angular 输出?

[英]How can I bind multiple actions to a single Angular output?

如何在不创建任何其他方法的情况下将多个动作绑定到单个 Angular 输出?

一个示例是根据自定义组件的输出更新formControl的值,并将该formControl标记为脏。

您可以将数组绑定到 HTML 模板中的输出,这使您能够触发多个操作:

模板:

<ul>
    <li style="cursor:pointer" 
        *ngFor="let value of values"
        (click)="[myFormControl.setValue(value), myFormControl.markAsDirty()]">{{value}}
    </li>
</ul>
<ul>
    <li>Form Control Value: {{myFormControl.value}}</li>
    <li>Form Control Dirty: {{myFormControl.dirty}}</li>
</ul>
<button (click)="myFormControl.reset()">Reset Form Control</button>

成分:

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
    myFormControl = new FormControl(null);
    values = ['spring', 'summer', 'autumn', 'winter'];
}

Stackblitz 示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM