簡體   English   中英

如何在回調函數的Angular 1.5組件中處理此問題?

[英]How to handle this in Angular 1.5 component in callback function?

我試圖將1.4 AngularJS指令重構為1.5組件。 我通過刪除$范圍,並用它替換這試試this

到目前為止,它工作正常,除了:我需要在回調函數中設置$scope變量。 像這樣:

this.variable = {};

someFunction().then(function(newValue) {
  this.variable = newValue;
});

但是, this在回調函數中是未定義的。

如何設置此this.variable的值的變通辦法或正確的方式呢?

您需要將范圍分配給您的函數:

this.variable = {};

someFunction().then(function(newValue) {
  this.variable = newValue;
}.bind(this));

this自己的函數中,指的是funtion本身,這就是為什么你得到了一個未定義。

將全局this.variable = {}更改為$scope.variable={}並在函數內部調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM