我在运行' 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">
答案 0 :(得分:1)
根据MDN push.apply需要从原型中完成。尝试:
Array.prototype.push.apply(
$scope.priceSummaryRequest.Groups[0].Products,
$scope.selectedProducts.Products);
或者你可以简单地说:
$scope.selectedProducts.Products.forEach(function(item){
$scope.priceSummaryRequest.Groups[0].Products.push(item);
});
答案 1 :(得分:1)
你可以这样做:
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);