简体   繁体   English

Angular和MEAN.js $ scope属性未定义

[英]Angular and MEAN.js $scope attributes are undefined

I am currently developing a little app with Angular and MEAN.js. 我目前正在使用Angular和MEAN.js开发一个小应用程序。 I would like to plot a D3 chart with some real data coming from the model. 我想绘制一个D3图表,其中包含来自模型的一些实际数据。 To do so, I would have to read from the $scope.campaign variable and initialize the $scope.dataBarChart with the data from $scope.campaign.tokens that is an array. 为此,我必须从$ scope.campaign变量中读取并使用$ scope.campaign.tokens中的数据(该数组为数组)初始化$ scope.dataBarChart。

// Find existing Campaign
$scope.findOne = function() {
    $scope.campaign = Campaigns.get({ 
        campaignId: $stateParams.campaignId
    });

    $scope.dataBarChart = [
        {
        'key': 'Token Requested',
        'values': [['10.10.2014', 550000], ['11.10.2014',300000]]
        }, {
        'key': 'Token Consumed',
        'values': [['10.10.2014', 250000], ['11.10.2014',200000]]
        }
    ];
};

When I try to log the value of $scope.campaign, I get all the data that I need. 当我尝试记录$ scope.campaign的值时,我得到了我需要的所有数据。 Unfortunately, when I try to access the $scope.campaign.tokens, I get an error like impossible to access tokens from undefined value. 不幸的是,当我尝试访问$ scope.campaign.tokens时,出现错误,例如无法从未定义的值访问令牌。 Basically, it seems that there is no data, but I know from the log that is not like this. 基本上,似乎没有数据,但是从日志中我知道不是这样的。

The complete code is simply the same, but with a console.log line 完整的代码是完全相同的,但是带有console.log行

// Find existing Campaign
$scope.findOne = function() {
$scope.campaign = Campaigns.get({ 
   campaignId: $stateParams.campaignId
});

console.log($scope.campaign)

$scope.dataBarChart = [{
   'key': 'Token Requested',
   'values': [['10.10.2014', 550000], ['11.10.2014',300000]]
   }, {
   'key': 'Token Consumed',
   'values': [['10.10.2014', 250000], ['11.10.2014',200000]]
}];
};

The console.log shows the right content, but when I try to use it $scope.campaign.tokens, it says undefined. console.log显示正确的内容,但是当我尝试使用$ scope.campaign.tokens时,它表示未定义。

Anyone suggestions? 有人建议吗? Thanks 谢谢

Try 尝试

$scope.campaign.$promise.then(function(data) {
   console.log(data.tokens)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM