简体   繁体   English

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

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

How can I bind multiple actions to a single Angular output, without creating any additional methods?如何在不创建任何其他方法的情况下将多个动作绑定到单个 Angular 输出?

An example would be updating the value of a formControl based on an output from a custom component and also marking that formControl as dirty.一个示例是根据自定义组件的输出更新formControl的值,并将该formControl标记为脏。

You can bind an array to the an output in your HTML template, which enables you to trigger multiple actions:您可以将数组绑定到 HTML 模板中的输出,这使您能够触发多个操作:

Template:模板:

<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>

Component:成分:

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 . Stackblitz 示例

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

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