2016-07-06 37 views
6

Sự kiện change trong Angular 2 là gì? Khi nào nó được gửi đi và tôi có thể sử dụng nó như thế nào?
I. e. tôi đã đăng ký mã sau đây qua số (change)="update()"?Sự kiện `thay đổi` trong Angular 2

http://plnkr.co/edit/mfoToOSLU6IU2zr0A8OB?p=preview

import {Component, View, Input, Output, EventEmitter, OnChanges} from '@angular/core' 

@Component({ 
    selector: 'inner-component', 
    template: ` 
    <label><input type="checkbox" [(ngModel)]="data.isSelected"> Selected</label> 
    ` 
}) 
export class InnerComponent { 
    data = { isSelected: false }; 
} 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <p><inner-component (change)="update()"></inner-component></p> 
    <p>The component was updated {{count}} times</p> 
    `, 
    directives: [InnerComponent] 
}) 
export class AppComponent { 
    count = 0; 

    update() { 
    ++this.count; 
    } 
} 

PS: Same question in Russian.

+0

Tôi rất ngạc nhiên khi nó hoạt động. Cảm thấy giống như một lỗi từ góc cạnh. – maxisam

Trả lời

12

Đó là sự kiện bọt: change lần xuất hiện trên input, sau đó bong bóng bằng cây dom và được xử lý trên phần tử inner-component. Nó có thể được kiểm tra bằng cách khai thác gỗ và sự kiện:

http://plnkr.co/edit/J8pRg3ow41PAqdMteKwg?p=preview

@Component({ 
    selector: 'my-app', 
    template: ` 
    <p><inner-component (change)="update($event)"></inner-component></p> 
    <p>The component was updated {{count}} times</p> 
    `, 
    directives: [InnerComponent] 
}) 
export class AppComponent { 
    count = 0; 

    update($event) { 
    console.log($event, $event.target, $event.currentTarget); 
    ++this.count; 
    } 
} 
+2

Vì vậy, chúng tôi thực sự có thể sử dụng (thay đổi) ở bất kỳ đâu. Điều đó thực sự làm tôi ngạc nhiên. – maxisam

3

Sự kiện change thông báo cho bạn về thay đổi xảy ra trong trường nhập. Kể từ khi thành phần bên trong của bạn không phải là một thành phần góc bản xứ, bạn phải thể chỉ định sự kiện này phát mình:

@Component({ 
    selector: 'inner-component', 
    template: ` 
    <label><input type="checkbox" (change)="inputChange.emit($event)" [(ngModel)]="data.isSelected"> Selected</label> 
    ` 
}) 
export class InnerComponent { 
    @Output('change') inputChange = new EventEmitter(); 

    data = { isSelected: false }; 
} 

Và trong AppComponent bạn ngay bây giờ bạn đang nhận được sự kiện:

@Component({ 
    selector: 'my-app', 
    template: ` 
    <p><inner-component (change)="update($event)"></inner-component></p> 
    <p>The component was updated {{count}} times</p> 
    `, 
    directives: [InnerComponent] 
}) 
export class AppComponent { 
    count = 0; 

    update(event: any) { 
    ++this.count; 
    console.log(event); 
    } 
} 
+1

[Mã của tôi] (// plnkr.co/edit/mfoToOSLU6IU2zr0A8OB?p=preview) ** đang hoạt động. Nếu bạn mở plunker, bạn sẽ thấy số lượng thay đổi gia tăng sau khi nhấp vào hộp kiểm. Trong [mã bạn] (// plnkr.co/edit/wCR7xS7ygEZijsN4B34W?p=preview) 'thay đổi' được gọi là ** hai lần ** trên mỗi lần nhấp vào hộp kiểm. – Qwertiy

+0

Tôi tìm thấy lý do te. Cảm ơn bạn đã làm việc với mã '(thay đổi)' trên đầu vào - trong phiên bản đầu tiên của tôi nó không hoạt động vì tôi đặt tên EventEmitter là 'thay đổi'. – Qwertiy

Các vấn đề liên quan