2015-07-04 73 views
-2

即时新手,我有问题。我使用promise angularjs编写简单的代码。(Angularjs Javascript)变量赋值

var data = {"collection": "installation", 
       "timeframe": { 
          "start": moment(date_from).format('YYYY-MM-DDTHH:mm:ss'), 
          "end": moment(date_to).format('YYYY-MM-DDTHH:mm:ss') 
       }}; 
    var install_all = Insight.installations(data); 
    data['filters'] = [{"property_name": "os", "operator": "eq", "property_value": "android"}]; 
    var filter_android = data; 
    var install_android = Insight.installations(filter_android); 
    data['filters'][0]['property_value'] = 'ios'; 
    var filter_ios = data; 
    var install_ios = Insight.installations(filter_ios); 
    data['filters'][0]['property_value'] = 'windowsphone'; 
    var filter_wp = data; 
    var install_windowsphone = Insight.installations(filter_wp); 
    $q.all({'install_all': install_all, 'install_android':install_android, 
    'install_ios':install_ios,'install_windowsphone':install_windowsphone}) 
    .then(function(data){ 
     $scope.install_all = data['install_all']['result']; 
     $scope.install_android = data['install_android']['result']; 
     $scope.install_ios = data['install_ios']['result']; 
     $scope.install_windowsphone = data['install_windowsphone']['result']; 

    }, function(errors){ 
     console.log("The request failed with response" + errors.status); 
    }); 

我希望当我运行,脚本调用4个API不同的过滤器。但是当我检查控制台日志,所有api相同的过滤器是这样的:

{u'filters': [{u'operator': u'eq', u'property_name': u'os', u'property_value': u'windowsphone'}], u'collection': u'installation', u'timeframe': {u'start': u'2015-07-04T00:00:00', u'end': u'2015-07-04T00:00:00'}} 
"POST /admin/api/queries/count HTTP/1.1" 200 - 

为什么变量相同?我不明白。对不起,我的英语

回答

0

您正在覆盖data过滤器。你总是修改同一个对象。 Filter-ios,filter-android,filter-wp是对同一对象的引用。在修改之前,您需要制作副本。

+0

我做了一个副本,但没有成功。我不知道为什么?你有一个例子吗?指定与JavaScript或PHP不同的var javascript –

+0

javascript中的变量赋值与任何其他语言中的赋值相同。原点和新变量名的引用是相同的。指定不是副本!如果你改变原来的,那么你也改变新的变量。在角度你使用'angular.copy'。 – Michael

+0

也许你应该再次阅读关于JavaScript中对象引用的章节。 – Michael