gpt4 book ai didi

javascript - 用 Angular 连接两个 json 数组时“对象不是函数”

转载 作者:行者123 更新时间:2023-12-02 16:23:56 24 4
gpt4 key购买 nike

当我多次运行下面的“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

关于javascript - 用 Angular 连接两个 json 数组时“对象不是函数”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com