如何将多个操作绑定到单个 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 example.