Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2范围问题_Angular_Typescript_Scope - Fatal编程技术网

Angular 角度2范围问题

Angular 角度2范围问题,angular,typescript,scope,Angular,Typescript,Scope,关于变量的作用域和访问这些变量中的值,我遇到了一个奇怪的问题 我基本上有这个(短样本)组件: import { Component } from '@angular/core'; import { HTTP_PROVIDERS } from '@angular/http'; import { InputText, DataTable, Button, Dialog, Column, Header, Footer, Panel, ProgressBar, Dropdown, SelectItem,

关于变量的作用域和访问这些变量中的值,我遇到了一个奇怪的问题

我基本上有这个(短样本)组件:

import { Component } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { InputText, DataTable, Button, Dialog, Column, Header, Footer, Panel, ProgressBar, Dropdown, SelectItem,
SplitButton, SplitButtonItem, Toolbar, SelectButton, OverlayPanel, Checkbox,
ToggleButton } from 'primeng/primeng';
import { User } from './../users.models';
import { UsersService } from './../users.service';
import { Router, ActivatedRoute }       from '@angular/router';
import { BbTile } from "./../../../shared/tile/tile.component";
import { BbTileModel } from "./../../../shared/tile/tile.model";

@Component({
    templateUrl: 'app/pages/users/dashboard/index.html',
    selector: 'brandbassador-app',
    // Honestly I'm not sure if all of these are required here
    directives: [(...), BbTile],
    providers: [HTTP_PROVIDERS, UsersService]
})

export class UsersDashboardComponent {
    // Data variables
    tiles: BbTileModel[] = [];
    // Statistics variables
    totalRevenue: number;
    usersCount: number;
    averageEngagementRate: number;
    totalReach: number;

    ngOnInit() {
        console.log("INIT    ");

        this.usersService.getUsers().then(
            users => {
                this.users = users;
                this.usersCount = this.users.length;
                this.getTotalRevenue();
                this.getAverageEngagementRate();
                this.getTotalReach();
                this.getMostActive();
                this.getTopPerformers();

                this.initTiles(); //The function to call
            });

        //Alternative call place
    }

    initTiles() {
        this.tiles.push(
            new BbTileModel("USERS", (this.usersCount).toString()),
            new BbTileModel("REVENUE", (this.totalRevenue).toString()),
            new BbTileModel("AVERAGE ENGAGMENT RATE", (this.averageEngagementRate).toString()),
            new BbTileModel("REACH", (this.totalReach).toString())
        );
    }
}
所以,这只是我的组件的一个片段。我在从InitTiles函数中访问局部变量时遇到问题

基本上,每当我在当前位置(注释为
要调用的函数
)上调用函数时,一切正常

但是,如果我将该调用移到
this.usersService.getUsers()之外,那么(…)
(注释为
备用调用位置的调用)我将失去从本地组件读取值的所有能力

为什么会这样?这是因为变量是在
getUsers
函数的作用域内分配的,还是在后台发生了一些关于作用域的其他问题

有人能详细说明一下吗?

什么时候

//Alternative call place
执行时,调用
getUsers()。然后(…)
尚未发生

这是异步执行的正常行为。

当异步调用的响应到达时,将执行传递给
然后(…)
的代码。
之后的代码(…)
会立即执行。

好的,所以我的错误是,我希望数据会在那里,而实际上它还不在那里。我想应该是类似的东西。所以我可以说在
之后(…)
我可以添加另一个
然后()
调用/初始化TileModel?你可以这样做,但是添加另一个
然后(…)
的目的是什么,当你可以将代码移动到
然后(…)
你已经有了。没有其他原因,除了成为一种强迫症:D