为什么我的数组仍然是空的,即使我在控制器中为这个数组赋值? (我使用的是angularjs,而非角度)

时间:2017-09-14 16:36:50

标签: angularjs typescript

这个问题花了一天时间来调试它,但仍然没有运气。

问题: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);

}

2 个答案:

答案 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

您可以通过getTemplateFromGivenRepothen能够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

通过这种方式,您将保证使用相同的范围实例

希望它能运作