Reputation: 53
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.
Upvotes: 0
Views: 897
Reputation: 27202
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 :
$scope.x = response.data.records
instead of $scope.x = []
$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>
Upvotes: 1
Reputation: 16540
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
Upvotes: 0
Reputation: 418
$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);
}
Upvotes: 0
Reputation: 21688
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);
});
}
Upvotes: 1