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

时间:2022-08-24 13:20:58

I want to change div automatic with $scope.push

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

but it still have problem with:

但它仍然有问题:

Cannot read property 'push' of undefined

无法读取undefined的属性'push'

My JSON and JAVASCRIPT codes:

我的JSON和JAVASCRIPT代码:

JSON

JSON

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

JAVASCRIPT

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

HTML

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

Note : I want to after submit plusCart , div automatically change the value with $scope.push

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

Thanks.

谢谢。

4 个解决方案

#1


1  

As per your JSON :

根据你的JSON:

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

response.data.records already having an array [{"total":"156000"}].So, there is no need to assign it again in an array.

response.data.records已经有一个数组[{“total”:“156000”}]。所以,没有必要在数组中再次分配它。

code correction :

代码修正:

  • Use only $scope.x = response.data.records instead of $scope.x = []
  • 仅使用$ scope.x = response.data.records而不是$ scope.x = []
  • Declare an array with name $scope.xtotal.
  • 声明名为$ scope.xtotal的数组。

Working Demo :

工作演示:

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>

#2


1  

as $scope.xtotal is not defined, a array push is showing error there

由于$ scope.xtotal未定义,因此数组推送显示错误

try below

试试下面

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

#3


0  

$scope.x Is an array. because {"records":[{"total":"156000"}]}

$ scope.x是一个数组。因为{“records”:[{“total”:“156000”}]}

If you want to assign all values from resonse.data.records you can simply do:

如果要分配来自resonse.data.records的所有值,您可以执行以下操作:

$scope.xtotal = response.data.records;

If you want to do manually, and yes you need to declare variable as an array:

如果你想手动完成,是的,你需要将变量声明为数组:

$scope.xtotal = [];
for (var i = 0; i < $scope.x.length; i++) {
    var obj = $scope.x[i];
    // do your modification for an obj
    // and push to your $scope.xtotal
    $scope.xtotal.push(obj);
}

#4


0  

Because $scope.x (which is set to response.data.records) is already the array you want

因为$ scope.x(设置为response.data.records)已经是你想要的数组

$scope.xtotal.push($scope.x.total);

should just be

应该只是

$scope.xtotal = $scope.x;

See plunker: http://plnkr.co/edit/fo1kffVaIUKe3DrRDODN?p=preview

请参阅plunker:http://plnkr.co/edit/fo1kffVaIUKe3DrRDODN?p = preview

#1


1  

As per your JSON :

根据你的JSON:

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

response.data.records already having an array [{"total":"156000"}].So, there is no need to assign it again in an array.

response.data.records已经有一个数组[{“total”:“156000”}]。所以,没有必要在数组中再次分配它。

code correction :

代码修正:

  • Use only $scope.x = response.data.records instead of $scope.x = []
  • 仅使用$ scope.x = response.data.records而不是$ scope.x = []
  • Declare an array with name $scope.xtotal.
  • 声明名为$ scope.xtotal的数组。

Working Demo :

工作演示:

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>

#2


1  

as $scope.xtotal is not defined, a array push is showing error there

由于$ scope.xtotal未定义,因此数组推送显示错误

try below

试试下面

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

#3


0  

$scope.x Is an array. because {"records":[{"total":"156000"}]}

$ scope.x是一个数组。因为{“records”:[{“total”:“156000”}]}

If you want to assign all values from resonse.data.records you can simply do:

如果要分配来自resonse.data.records的所有值,您可以执行以下操作:

$scope.xtotal = response.data.records;

If you want to do manually, and yes you need to declare variable as an array:

如果你想手动完成,是的,你需要将变量声明为数组:

$scope.xtotal = [];
for (var i = 0; i < $scope.x.length; i++) {
    var obj = $scope.x[i];
    // do your modification for an obj
    // and push to your $scope.xtotal
    $scope.xtotal.push(obj);
}

#4


0  

Because $scope.x (which is set to response.data.records) is already the array you want

因为$ scope.x(设置为response.data.records)已经是你想要的数组

$scope.xtotal.push($scope.x.total);

should just be

应该只是

$scope.xtotal = $scope.x;

See plunker: http://plnkr.co/edit/fo1kffVaIUKe3DrRDODN?p=preview

请参阅plunker:http://plnkr.co/edit/fo1kffVaIUKe3DrRDODN?p = preview