這個問題花了我一天調試它,但仍沒有運氣。爲什麼我的數組仍然是空的,即使我已經在控制器中爲這個數組賦值了? (我用angularjs,沒有棱角)
問題:this.gridOptions.data = this.allTemplatesFromClassificationRepo ;
** this.allTemplatesFromClassificationRepo **仍然是一個空數組。在這行代碼執行之前,我已經調用activate()函數將數組賦給** this.allTemplatesFromClassificationRepo **。請參閱此行this.allTemplatesFromClassificationRepo = templateReorderProperties;
附:雖然我無法在控制器中獲得allTemplatesFromClassificationRepo
的值,但我可以在html中獲得它的值。
namespace app.admin {
'use strict';
class FooController {
static $inject: Array<string> = [];
constructor() {
this.activate();
}
activate() {
this.getData();
this.classificationFoo = this.data[0];
this.getTemplateFromGivenRepo(this.classificationFoo.id, this.classificationFoo.displayName);
this.populateData();
}
data: any;
classificationFoo: any;
allDataFromclassificationFoo: any = [];
// demo grid
gridOptions = {
enableFiltering: true,
},
data: []
};
populateData() {
this.gridOptions.data = this.allTemplatesFromClassificationRepo ;
}
getData() {
this.fooService.getUserData();
this.data = this.fooService.userdata;
}
getTemplateFromGivenRepo(fooId: string, fooName: string) {
switch (fooId) {
case 'FOO':
this.TemplateApi.templatesAvaiableForRepoIdGET(fooId).then(data => {
data.forEach(element => {
element.fooName = fooName;
});
let templateReorderProperties = data
this.allTemplatesFromClassificationRepo = templateReorderProperties;
}, error => {
});
break;
default:
break;
}
};
}
class Bar implements ng.IDirective {
static $inject: Array<string> = [];
constructor() {
}
bindToController: boolean = true;
controller = FooController;
controllerAs: string = 'vm';
templateUrl: string = 'app/foo.html';
static instance(): ng.IDirective {
return new Bar();
}
}
angular
.module('app.admin')
.directive('bar', Bar.instance);
}
我懷疑函數中'this this'this.allTemplatesFromClassificationRepo'寫的是與你期望的不同的'this'。 (@see:https://stackoverflow.com/questions/36489579/this-within-es6-class-method) – birdspider
@birdspider有我的解決方案嗎?我不知道如何解決它,我已經盡了全力 – EntryLeveDeveloper
爲了檢驗這一理論對「這個」嘗試略高於切換(方法的第1行)和寫'讓_this = this',當你指定數組做'_this 。 allTemplatesFromClassificationRepo = templateReorderProperties' – cYrixmorten