Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This is my controller code

var myApp = angular.module('myApp',[]);
myApp.controller('restaurantController',['$scope','$http', function($scope, $http){
    $http.get($scope.something).success(function (data){
        $scope.items = data;
    });
    $scope.orders = [];
    $scope.process = function(item){
        var cart = '{"name":"'+item.name+'","price":"'+item.quantity*parseInt(item.price)+'","quantity":"'+item.quantity+'"}';
        $scope.orders.push(cart);
    }
}]);

Basically I have a PHP page where I am getting the user values and dynamically adding elements to the $scope.orders array.

And then to display the elements of array I am using this code

<div class="container" ng-repeat="order in orders">
    <h3>{{order.name}}</h3>
</div>

In my PHP page.But nothing being displayed.

share|improve this question
3  
Because cart is a string, not object – Tushar 23 hours ago
    
Problem is not because of String. you are not sending item from DOM or if you want to set it from items which you already called in success callback then use forEach loop for seperate entries of item – ojus kulkarni 23 hours ago
up vote 1 down vote accepted

Be careful you're not pushing object in your $scope.orders Array, you're pushing stringified version of your object.

Unlike PHP, JavaScript interpret and nows how to use and browse through JSON objects. Try this :

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

myApp.controller('restaurantController',['$scope','$http', function($scope, $http){
  $scope.orders = [];

  $http.get($scope.something)    // .success() is deprecated use .then() instead
    .then(function ( data ) {
      $scope.items = data;
    },
    function ( err ) {
      $scope.items = [];
    });

  $scope.process = function(item){
    var cart = { 
      name      : item.name,
      price     : item.quantity * parseFloat(item.price), 
      quantity  : item.quantity 
    };

    // Use .parseFLoat() because item price may not be an integer

    $scope.orders.push(cart);
  }
}]);

Then you'll be able to loop over $scope.orders Array.

share|improve this answer
    
Thank you its working.I didnt notice that I am passing a string. – Gopal Chandak 19 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.