2013-10-11 158 views
0

我正在使用全局变量与外部javascript进行通信。所以我用AngularJs清除全局变量

window.report = $scope.myData // my scope with all data 

问题是,当我尝试运行报告行吟诗人,我应该首先重置这个全局变量,然后再填写一次。

我曾尝试用:

window.report = null; 

window.report = {}; 

但旧的数据仍然存在,如果没有新的数据覆盖....

可能是这部分模板缓存引起该问题?

我试图登录控制台的window.report变量,并且是未定义的。所以,问题应该是在别处......

[更新]

这里的问题可能是一个服务。

app.factory('Report', ['$http', function($http,$q){ 

var Reports = { 

    reports : {}, 

    requests :[{'url':'getReport','response':'Analizing page','count':1}, 
       {'url':'getPagerank','response':'Getting 1','count':2}, 
       {'url':'getRobots','response':'Getting 2','count':3}, 
       {'url':'getIpCanonicalization','response':'Gwetting 3','count':4}] 

       ] 

}; 

Reports.getReport = function(target, source, response, callback) { 
    return $http({ url:"/seo/getter/", 
        method:"POST", 
        //cache: true, 
        params:{"url" : target, "action": source} 
       }).success(function(result) { 
        callback(result); 
        console.log(Reports.reports) 
        jQuery.extend(true,Reports.reports, result.data) 
        //console.log($scope.user) 
       }).error(function(error){ 
        callback(result); 
        jQuery.extend(true,Reports.reports, result.data) 
       }) 
} 

    Reports.startQueue = function (target, callback) { 
     var promises = []; 
     this.requests.forEach(function (obj, i) { 
      promises.push(Reports.getReport(target, obj.url, obj.response, function(response,reports){ 
       callback(obj.response,Reports.reports,obj.count) 
      })); 
     }); 
    } 

return Reports; 
}]) 

我认为Reports.reports var仍然包含旧数据,当我尝试更新我的视图。所以这个问题不应该是全球性的变数,而应该是一个仍然占据以前数据的服务。我如何确保Report.reports在更新时为空?

+1

尝试在角码$窗口,这样你的角度可以访问到window对象。你可以尝试不断的 – Whisher

+0

总是尝试!可能是缓存问题? – Tropicalista

+0

尝试发布更多的代码 – Whisher

回答

0

我已经解决了添加新的功能,我的服务,所有的过程之后重置我的服务VAR:

Reports.resetReports = function() { 
     Reports.reports = {}; 
    } 
0

不要使用窗口只是不要把VAR放在它的前面。你也无法删除一个全局变量,只是基本的JavaScript。