2016-10-27 13 views
0

内結合インスタンス変数にアクセス:Angular2は、私は次のコンポーネントを持ってangular2では、コールバック

import { Component } from '@angular/core'; 

const dialog = require("electron").dialog; 
const xml2js = require('xml2js'); 
const fs = require("fs"); 
const ipc = require('electron').ipcRenderer; 

@Component({ 
    selector: 'ct-config-editor', 
    templateUrl: 'config.editor.component.html' 
}) 
export class ConfigEditorComponent { 

    constructor() { 
    this.selected_file = 'Max'; 
    } 


    clicked(event){ 
    alert("lol"); 
     ipc.send('open-file-dialog'); 

     ipc.on('selected-directory', function (event, path) { 
     this.selected_file = `You selected: ${path}`; 
     }); 
    } 
} 

ビューは、このようなselected_file呼ば正しくバインドプロパティを持っています

<h1>{{selected_file}}</h1> 

H1の値であります開始時の最大値 - コールバックが実行された後、 'this'のコンテキストが自分のクラスではないため、this.selected_fileにアクセスすることはできません。

コールバック内でインスタンス変数にアクセスするにはどうすればよいですか?

答えて

2

使用状況を保持する矢印機能:

ipc.on('selected-directory', (event, path) => { 
    this.selected_file = `You selected: ${path}`; 
}); 

この方法でthisは、あなたのクラスに参照される

は、ここでは詳細

+0

はこれですも参照してください。ベストプラクティス?私は特に矢印の機能が何をしているのか理解していません! – RenegadeAndy

関連する問題