gpt4 book ai didi

javascript - 无法读取未定义的 Angular JS 的属性 'push'

转载 作者:行者123 更新时间:2023-12-02 13:51:44 25 4
gpt4 key购买 nike

我想用$scope.push自动更改div

但仍然有问题:

无法读取未定义的属性“push”

我的 JSON 和 JAVASCRIPT 代码:

JSON

{"records":[{"total":"156000"}]}

JAVASCRIPT

 $scope.plusCart = function() {
$http({
method : 'POST',
url : 'http://localhost/so-ku/www/server/menu/plus_cart',
headers : { 'Content-Type' : 'application/json' },
data : JSON.stringify({ rowid: $scope.currentItem.rowid, qty : $scope.currentItem.qty })
}).success(function(data) {
total_cart()
});

}
function total_cart() {
$http.get('http://localhost/so-ku/www/server/menu/total_cart').
then(function(response){
$scope.x = [];
$scope.x = response.data.records;
$scope.xtotal.push($scope.x.total);
});
}

html

<div ng-controller="CartCtrl">
<div ng-repeat="tot in xtotal" style="padding-top: 10px;">
Rp {{tot.total}}
</div>
</div>

注意:我想在提交 plusCart 后,div 自动使用 $scope.push 更改值

谢谢。

最佳答案

根据您的JSON:

{"records":[{"total":"156000"}]}

response.data.records 已经有一个数组 [{"total":"156000"}]。因此,无需在数组。

代码更正:

  • 仅使用 $scope.x = response.data.records 而不是 $scope.x = []
  • 声明一个名为 $scope.xtotal 的数组。

工作演示:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
var jsonObj = {
"records":[{"total":"156000"}]
};

$scope.xtotal = [];
$scope.x = jsonObj.records;
$scope.xtotal.push($scope.x[0].total);
console.log($scope.xtotal);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="item in xtotal">
{{item}}
</div>
</div>

关于javascript - 无法读取未定义的 Angular JS 的属性 'push',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40986054/

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