gpt4 book ai didi

javascript - Angular : How to convert values in array and get value in outside scope?

转载 作者:行者123 更新时间:2023-11-29 20:47:06 24 4
gpt4 key购买 nike

DynamicDatabase 类中,我从服务器获取值并在 console.log(this.rootLevelNodes)//folder-1 folder-2 folder-3 folder-4 请参阅下面的代码,但我希望这个值在数组中。然后在 this.route 外,我打印 console.log(this.rootLevelNodes)//我在函数中得到了 null [] (this.rootLevelNodes) 但我不知道为什么我得到了 null 结果 但是我也想要数组格式的结果怎么可能看到我下面的代码并帮助我?

@Injectable()
export class DynamicDatabase {

rootLevelNodes: string[] = [];
constructor(private route : ActivatedRoute, private userService : UserService){}
upperFolderId;
urlOfFolder;

initialData(): DynamicFlatNode[] {

this.route.params.subscribe(params => {

this.upperFolderId = params['userid'];
this.urlOfFolder = params['url']+'/'+this.upperFolderId;

this.userService.getPatternMainFolder({'urlOfFolder' : this.urlOfFolder}).subscribe(
(data) => {
if(data != undefined && data != '' && data.payload != undefined && data.payload != ''){
for(var i = 0 ; i < data.payload.length ; i++){
this.rootLevelNodes = data.payload[i].folderName;
console.log(this.rootLevelNodes); // folder-1
folder-2
folder-3
folder-4
<-- this type result I got

// I want this type result
console.log(this.rootLevelNodes); // ['folder-1','folder-2','folder-3','folder-4']
}
}
}
)
});

console.log(this.rootLevelNodes); // [] --> Here I result got null array

// Here also I want this type result want
console.log(this.rootLevelNodes); // ['folder-1','folder-2','folder-3','folder-4']
return this.rootLevelNodes.map(name => new DynamicFlatNode(name, 0, true));
}

最佳答案

我会将单个函数分成两部分:

initialData() {
this.route.params.subscribe(params => {
this.upperFolderId = params['userid'];
this.urlOfFolder = params['url']+'/'+this.upperFolderId;
this.anotherFunction(this.urlOfFolder);
});
}

anotherFunction(data) {
this.userService.getPatternMainFolder({'urlOfFolder' : data}).subscribe(
(data) => {
if(data != undefined && data != '' && data.payload != undefined && data.payload != ''){
for(var i = 0 ; i < data.payload.length ; i++){
this.rootLevelNodes.push(data.payload[i].folderName);
console.log(this.rootLevelNodes);
}
}
return this.rootLevelNodes;
}
)
}

关于javascript - Angular : How to convert values in array and get value in outside scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54006189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com