Angularjs - 如何在函数外部访问$ scope变量

时间:2015-07-17 19:26:50

标签: angularjs

 angular.module('finalApp').controller('WarningsCtrl', function ($scope,HttpService) {
    HttpService.get('/warings/snowstrome').error(function(data){
        data={
            values:[
            {
                wariningtype:'snowstrome',
                warningtitle: 'Severe Snowstrom Warning',
                agentid: 100,
                name: 'Otaru-Shi',
                location:'Hokkaido',
                timezone: 'Feb 16,4:35pm Jst',
                zip:'0470028, 0470035, 0470154',
                weatherstatus:'Snow, Ice and Low Temperature Expected',
                weatheralerts:'Automated Alert Sent to 45 Customers',
                affectedcustomers:'Affected Customers HR:20, MR:20, LR:20'
            }
            ]
        };
        $scope.liveCards=data;
    });
    console.log($scope.liveCards)
});

1 个答案:

答案 0 :(得分:1)

$ scope可在get call之外访问。由于AJAX调用的异步性,控制台不得显示​​更新的数据。

您可以尝试更新以下功能

angular.module('finalApp').controller('WarningsCtrl', function ($scope,HttpService) {
  var getSnowStromeWarning = function(callback) {
        HttpService.get('/warings/snowstrome').error(function(data){
        data={
         ...   
        };
        $scope.liveCards=data;
        callback();
    });
   }  
   getSnowStromeWarning(function(){
       console.log($scope.liveCards)
   });
});