这个问题花了一天时间来调试它,但仍然没有运气。
问题:this.gridOptions.data = this.allTemplatesFromClassificationRepo ;
** this.allTemplatesFromClassificationRepo **仍然是一个空数组。在执行这行代码之前,我调用activate()函数将数组赋给** this.allTemplatesFromClassificationRepo **。请参阅此行this.allTemplatesFromClassificationRepo = templateReorderProperties;
P.S。虽然我无法在控制器中获得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);
}
答案 0 :(得分:2)
getTemplateFromGivenRepo
是异步操作。
在
之后在this.populateGridData();
内移动getTemplateFromGivenRepo
来电
this.allTemplatesFromClassificationRepo = templateReorderProperties;
getTemplateFromGivenRepo(repoId: string, repoName: string) {
switch (repoId) {
case 'CLASSIFICATION':
this.TemplateApi.templatesAvaiableForRepoIdGET(repoId).then(data => {
this.allTemplatesFromClassificationRepo = templateReorderProperties;
this.populateGridData(); // call here
}, error => {
});
}
};
OR
您可以通过getTemplateFromGivenRepo
和then
能够success
回拨来回复承诺,致电
this.populateGridData();
答案 1 :(得分:0)
我认为问题出现在 2 "phrase 1"
1 "phrase 2"
1 "phrase 3"
2 "phrase 4"
实例中,Promise内部的解析不同。
尝试写下类似的内容:
this
然后将所有var self = this;
更改为this
密钥。
a.e:
self
通过这种方式,您将保证使用相同的范围实例
希望它能运作