Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am new to angular js, I am using angular js with mvc5 application. I have created a module and controller in the js for angular. I have one action "View" in customer controller(MVC5). We need to show the all the customer in this view and i want to use "ng-repeat" here.

My problem is i am getting collection of customer as model, previously i was making foreach loop of model to show the customers. Now how i can add model into $scope data container so that i can use in ng-repeat.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

What you have been doing till now is rendering the customers collection on server straight to the HTML returned to the client. What you want to do ideally is to render the HTML without the customers collection and have a call over $http service to your API controller to give you the data. From there is is easy, you

I suggest reading:

Using $http in your controllers is far away from the best practice, but for simplification...the $http call in your controller could look like this:

$http({method: 'GET', url: '/api/customers'}).
    success(function(data, status, headers, config) {
        $scope.customers = data;
    }).
    error(function(data, status, headers, config) {
      alert('error');
    });

Then you ngRepeat in the view:

  <ul>
    <li ng-repeat="customer in customers">
       {{customer.name}}
    </li>
  </ul>
share|improve this answer
    
As of now i am not using web api, so you mean i need to call the MVC actions via web api? – Fooker Sep 2 '14 at 9:19
    
You must call WebAPI from Angular to retrieve the customers in JSON format. I strongly recommend using WebAPI, which can be part of the same project as your MVC, but you can get JSON result from MVC actions too: stackoverflow.com/questions/227624/… – David Bohunek Sep 2 '14 at 9:22
    
Thanks David :D – Fooker Sep 2 '14 at 9:56

For getting the data you need the http module to make call to server https://docs.angularjs.org/api/ng/service/$http

share|improve this answer

you should be doing this via an api request to an MVC controller function that is returning json to your frontend and than use the data with $http or $resource to feed it into your controller.

share|improve this answer

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.