2017-04-10 15 views
9

Tôi muốn gọi phương thức cha mẹ (deletePhone) trong thành phần con trong Angular 4. Làm cách nào để tôi có thể làm điều đó đúng cách?Góc 4 gọi phương thức cha mẹ trong một thành phần con

thành phần cha mẹ của tôi trông giống như:

export class ContactInfo implements OnInit { 
    phoneForm: FormGroup; 
    phones: Phone[]; 


    constructor(private fb: FormBuilder, 
      private userService: UserService) { 
    } 

    ngOnInit() { 
     this.userService.getDataPhones().subscribe(
      phones => { 
       this.phones = phones; 
      }); 

     this.phoneForm = this.fb.group({ 
      phone: ['', [Validators.pattern(PHONE_PATTERN)]] 
     }); 
    } 

    deletePhone(phone: Phone) { 
     this.userService.deleteUserPhone(phone) 
      .subscribe(res => { 
       let index = this.phones.indexOf(phone); 
       if (index > -1) { 
        this.phones.splice(index, 1); 
       } 
     }); 
    } 
} 

Trả lời

22
class ChildComponent { 
    @Output() someEvent = new EventEmitter<string>(); 

    callParent() { 
    this.someEvent.next('somePhone'); 
    } 
} 

Trong ContactInfo 's mẫu

<child-component (someEvent)="deletePhone($event)" 
+0

Heh! nó khá đơn giản, cảm ơn!)) –

+1

Bạn có thể thêm loại dự định như "EventEmitter mới ()" :) – Nicolas

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