0

I want to change div automatic with $scope.push

but it still have problem with:

Cannot read property 'push' of undefined

My JSON and JAVASCRIPT codes:

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>

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

Thanks.

PaladiN
  • 4,625
  • 8
  • 41
  • 66
Azizal Yunan P
  • 53
  • 1
  • 1
  • 9

4 Answers4

1

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

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);
    });
    }
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
1

As per your 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.

code correction :

  • Use only $scope.x = response.data.records instead of $scope.x = []
  • Declare an array with name $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>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

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

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

$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);
}
Akbar Kautsar
  • 416
  • 5
  • 13
  • yes i know ,but when i submit post data ,
    is not automatic refresh bro
    – Azizal Yunan P Dec 06 '16 at 03:53
  • I see, so after submitting the form, you are expecting total is updated in frontend? May be there are an error when submiting the form data. maybe you can add .error in your post method `$http({}).success(function(){}).error(function(){});` And try to log something in error section Also just my suggestion please use **.then** rather than **.success** Please refer [here](http://www.dwmkerr.com/promises-in-angularjs-the-definitive-guide/#promisessuccesserrorthen) and [here](http://stackoverflow.com/questions/38627623/angularjs-http-success-vs-then) – Akbar Kautsar Dec 06 '16 at 05:35
0

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

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

should just be

$scope.xtotal = $scope.x;

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

K Scandrett
  • 16,390
  • 4
  • 40
  • 65