2017-10-06 23 views
1

我在組件中使用了ng2-smart-table。Angular 4,使用ng2-smart表的回調函數'onComponentInitFunction'時,'this'是未定義的

我試圖調用父組件的approveTheOrder()方法,但我無法得到父類

下面的對象是片段

{ 
    title: "Actions", 
    type: "custom", 
    renderComponent: ActionRenderComponent, 
    onComponentInitFunction(instance) { 
    instance.actionEmitter.subscribe(row => { 
     if(row == CONSTANT.STATUS_APPROVE){ 
     //here 'row' value is from childComponent-ActionRenderComponent,which I am able to get easily in parentComponet 
     this.approveTheOrder(instance.rowData.id); // here this is undefined, 

     } 
     if(row == CONSTANT.STATUS_REJECT){ 
     this.rejectOrder();//this is undefined 
     } 
     if(row == CONSTANT.STATUS_PENDING){ 
     this.pendingOrder(); // this is undefined 
     } 
    }); 
    }, 

    filter: false 
}; 

有沒有人有任何想法如何在下面的onComponentInitFunction()中獲得'this'?

下面是我得到的錯誤圖像。 enter image description here

另外我試圖用'綁定'功能實現目標不成功,誰能請指導我在這裏,我真的卡在這一點。

編輯1 請注意,我能夠在爲父級ChildComponent取得事件,但這裏的問題是特定於NG2智能表的組成部分。

ChildComponent獲得的價值,我想用在建造回調函數onComponentInitFunction(實例)NG2智能表組件的

+0

@Yurzui - 是你給出的解決方案爲我工作。請隨時回答這個問題,這樣就不會有其他人陷入這個問題 –

+0

@ Z.Bagley - 這個問題是特定於ng-smart-table的,我能夠從ChildComponent獲取值到ParentComponent的構造器中。但問題出現在onComponentInitFunction()函數中。 –

+0

道歉,誤解和刪除。 –

回答

4

的語法如下:

{ 
    onComponentInitFunction(instance) { 
    ... 

將被轉換爲函數表達式:

{ 
    onComponentInitFunction: function(instance) { 
    ... 

您應該使用箭頭功能保留this

onComponentInitFunction: (instance) => { 
相關問題