我多次在下面运行“ getPriceSummary”功能时遇到错误。我从UI调用该函数,然后将JSON的一部分添加到另一部分,然后将其发送到API。

   (function() {
     var quoteBuild = angular.module('quoteApp');
     quoteBuild.controller('builderController', function($scope) {

       $scope.priceSummaryRequest = {
         "Groups": [{
             "Products": []
           },
           // other stuff
         ]
       };

       // this is dynamically created in the UI - this is just an examply of whats created
       $scope.selectedProducts.Products = [{
         "ProductCode": "Code1",
       }, {
         "ProductCode": "Code1",
       }, ]

       $scope.getPriceSummary = function() {

         $scope.priceSummaryRequest.Groups[0].Products.push.apply($scope.priceSummaryRequest.Groups[0].Products, $scope.selectedProducts.Products);
         // get prices back from the api
         productsServices.getSolutionPrice($scope.priceSummaryRequest)
           .then(function(res) {})
           .finally(function() {});
       }
     });
   }());


如前所述,第一次$ scope.getPriceSummary运行它就可以了,但是如果我再次运行它,我会得到这个错误

TypeError: object is not a function
at hb.functionCall (https://ajax.googleapis....)
at Cc.(anonymous function).compile.d.on.f (https://ajax.googleapis....)
at l.$get.l.$eval (https://ajax.googleapis....)
at l.$get.l.$apply (https://ajax.googleapis....)
at HTMLTableRowElement.<anonymous> (https://ajax.googleapis...)
at HTMLTableRowElement.n.event.dispatch (https://ajax.googleapis...)
at HTMLTableRowElement.n.event.add.r.handle (https://ajax.googleapis...)
(anonymous function)angular.js:8548 $getangular.js:14489     $get.l.$applyangular.js:21427 (anonymous function)jquery.min.js:3 n.event.dispatchjquery.min.js:3 n.event.add.r.handle


我认为这与我在哪里执行push.apply有关。有什么想法我做错了吗?

编辑
我不确定这是否相关,但是我从这样的表行调用getPriceSummary函数

<tr ng-click="getPriceSummary(I pass prices in here - just removed it)"  ng-repeat="prices in productVariant.Prices">

最佳答案

您可以这样做:

  var priceSummaryRequest = {
    "Groups": [{
        "Products": []
      },
      // other stuff
    ]
  };

  var selectedProducts = {};
  selectedProducts.Products = [{
    "ProductCode": "Code1",
  }, {
    "ProductCode": "Code1",
  }, ];

  selectedProducts.Products.forEach(function(product) {
    priceSummaryRequest.Groups[0].Products.push(product);
  });

  console.dir(priceSummaryRequest.Groups[0].Products);


http://plnkr.co/edit/IuneSeIIGQjby2V8rlZK?p=catalogue

09-17 21:36