2016-10-20 4 views
1

ファイルの内容をWebページに表示しようとしています。 Angularの$ httpディレクティブを使用してファイルを呼び出しています。

$http.get('p/About.java').success(function(data) { 
    console.log(data); // This prints the contents of the file. 
    this.about = data; // This does nothing. 
}); 

ファイルの内容がコンソールに表示されます。ただし、表示には表示されません。

foo{{$ctrl.about}}bar 

ビューには、foobarが表示されます。コンソールで印刷しても応答が印刷されないのはなぜですか? .success関数内

this
+1

コンテキストがあなたのコントローラと異なっています –

答えて

1

(コンテキスト)は、明示的に外側の範囲に囲まれていない限り、JavaScriptで各機能は、独自のコンテキストを持っているので、コントローラ機能のコンテキスト(this)とは異なります。 (矢印の機能を使用して)

var self = this; //at the top of your controller, then use self instead of this 
.... 
$http.get('p/About.java').success(function(data) { 
    console.log(data); // This prints the contents of the file. 
    self.about = data; // This does nothing. 
}); 

ES6バージョン$ HTTPのthis`内部 `の

$http.get('p/About.java').success(data => { 
    console.log(data); // This prints the contents of the file. 
    self.about = data; // This does nothing. 
}); 
関連する問題