【问题标题】:How to get selected option in change callback of AngularDart component如何在 AngularDart 组件的更改回调中获取选定的选项
【发布时间】:2018-05-25 14:08:34
【问题描述】:

我想使用一个 select HTML 元素来过滤表格中的项目。为此,我有一个模型值selectedCategoryId 和事件回调onFilterCategory 用于更改事件。但是当回调被调用时,selectedCategoryId 的值是null

我有以下 HTML sn-p:

<select id="category"
        class="form-control"
        [(ngModel)]="selectedCategoryId"
        (change)="onFilterCategory()">
    <option *ngFor="let category of categories"
            value="{{category.id}}">
        {{category.name}}
    </option>
</select>

还有下面的 dart sn-p:

void onFilterCategory() {
    print('onFilterCategory');
    print('this.selectedCategoryId: ' + this.selectedCategoryId);
}

我需要使用另一个回调吗?

【问题讨论】:

    标签: dart angular-dart


    【解决方案1】:

    ngModelChange 是事件,$event 是值

    (ngModelChange)="onFilterCategory($event)"
    

     void onFilterCategory(String value) {
    

    因为你有2路绑定

    [(ngModel)]="selectedCategoryId"
    

    你也可以使用

    (ngModelChange)="onFilterCategory()"
    

    onFilterCategory() 在您的问题中。

    change 事件不起作用,因为它触发得太早 - 在 [(ngModel)]="selectedCategoryId" 能够更新 selectedCategoryId 之前。

    【讨论】:

    • 当我将[(ngModel)](ngModelChange) 结合使用时,我收到编译器警告:ParseErrorLevel.FATAL: Found multiple events with the same name: ngModelChange. You should merge the handlers into a single statement. [(ngModel)]="selectedCategoryId"
    • 编译器警告是什么?
    • 这些是我的依赖项:angular: ^5.0.0-alphaangular_forms: ^2.0.0-alpha
    • 这是警告:ParseErrorLevel.FATAL: Found multiple events with the same name: ngModelChange. You should merge the handlers into a single statement. [(ngModel)]="selectedCategoryId"
    • 我明白了。那是新的。然后将[(ngModel)]="selectedCategoryId"更改为[ngModel]="selectedCategoryId"并使用(ngModelChange)="onFilterCategory($event)"
    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多