2015-11-25 40 views
6

Tôi đã đính kèm thành phần hộp thoại làm chỉ thị để hiển thị nó trên trang thành phần chính khi tôi nhấp vào nút trên trang chính (liên kết đến thành phần chính). Đây là cách tôi đã làm nóGóc 2 - Cách truy cập chỉ thị từ Component

Trong mẫu

<button id="goToTasksCases" class="btn btn-success btn-lg" (click)="doShowStartNewCase($event)">START A NEW CASE</button> 
<modal-new-case></modal-new-case> 

Trong thành phần

@Component({ 
    selector: 'case-index' 
}) 
@View({ 
    templateUrl: 'client/app/case/case.index.html', 
    directives : [ModalNewCaseComponent] 
}) 
export class CaseIndexComponent { 
    doShowStartNewCase(event: any) { 
     // how can I access the ModalNewCaseComponent 
    }  
} 

Tuy nhiên, tôi cần phải thiết lập một số giá trị cho các thành phần con (ModalNewCaseComponent) sau một số cuộc gọi lại từ dịch vụ Rest. Làm thế nào tôi có thể đạt được điều đó với thiết lập hiện tại?

Trả lời

14

Bạn có thể truy vấn các trẻ em xem bằng cách:

@Component({ 
    selector: 'case-index', 
    templateUrl: 'client/app/case/case.index.html', 
    directives : [ModalNewCaseComponent] 
}) 
export class CaseIndexComponent { 
    @ViewChild(ModalNewCaseComponent) 
    modal: ModalNewCaseComponent; 

    afterViewInit() { 
     // this.modal will have value 
    } 

    doShowStartNewCase(event: any) { 
     // how can I access the ModalNewCaseComponent 
    }  
} 

Bạn có thể tìm thêm về ViewChildren and ContentChildren here.

+0

jesus !! Tôi tìm kiếm google cho giờ cuối cùng và không tìm thấy bất kỳ điều gì gần với điều này (chủ yếu là đến với viewManager, phần tử ....). Bạn là một người bảo vệ cuộc sống !!!!! –

+1

Nó sẽ được giải thích thêm tại đây https://www.packtpub.com/web-development/switching-angular-2 :-) –

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