Angular 2处理数据异步

时间:2016-11-09 09:25:04

标签: asynchronous angular

我对Angular 2很新,我有一点问题: 我从服务asynrochnus加载数据。 数据到达我的组件,我可以处理它。 在这里,我将数据保存在局部变量中。

以下是组件中的代码:

export class ConfigurationComponent implements OnInit {

    private tab: string = 'general';
    private config = [];

    constructor(private router: Router, private variables: Variables, private apiService: ApiService) {
        if (!this.variables.getIsLoggedIn()) {
            this.router.navigate(['login']);
        }
    }

    ngOnInit() {
        this.getConfigService();
    }

    getConfigService() {
       this.apiService.getConfigService().
        subscribe(
            data => {
                this.config = data.settings.section;
            },
            //error => this.error = <any>error,
            error => this.variables.setFailure(error)
            //() =>  console.log('done:' + this.status)
         );
    }

这是html中的代码:

<th>{{config[5].entry.name}}</th>
<th>{{config[5].entry.text}}</th>

问题是,在视图加载时,局部变量'config'只是'[]'。所以'config [5]'失败了。 但是如果我没有在组件中定义局部变量,它也会失败。

有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该将 textAreaCode.addMouseWheelHandler(new MouseWheelHandler() { @Override public void onMouseWheel(MouseWheelEvent event) { textAreaNumbers.getElement().setScrollTop(textAreaCode.getElement().getScrollTop()); } }); 中的每个元素推送到data.settings.section并检查它是否存在于视图中;

config

我希望它有所帮助!