$ onInit

时间:2017-08-21 22:42:35

标签: javascript angularjs

我试图从绑定中访问数据("月"):

.component('component', {
    bindings: {
        months: '<',
    },
    templateUrl: 'url',
    controller: 'controller'
})

.controller('controller', function() {

    var vm = this;

    this.$onInit = function() {
        console.log(vm.months, vm.months.length, vm.months instanceof Array);
        vm.months.forEach(function(month) {
            console.log(month);
        });
    };
});

第一个console.log语句打印出一些奇怪的输出:

screenshot of developer tools console output

数据存在(包含12个项目的数组),但长度为零(?!)。这一点我根本不明白。

当我尝试遍历数组时,可能存在也可能不存在,每个月都不会在控制台中打印出来(请参阅forEach循环的内部主体)。我不能为我的生活弄清楚为什么会这样。我在这里缺少什么?

0 个答案:

没有答案
相关问题