2

I am new with angular, so i hope this question is not to stupid.
as the title says, i wish to populate a list using angular and a method from a mvc controller. But i cannot figure out how to call the method? i have tried making a http request but i cannot get i to work.
Am i totally lost or is it somthing like this below?
Here are the list.

<div ng-app="cardList" ng-controller="CardController"></div>
<p><input type="text" ng-model="test" /></p>

<ul>
    <li ng-repeat="x in names | filter:test">
        {{x}}
    </li>
</ul>

and here is the script.

<script>
var list = $http({ url: "Controllers/CardController/AllCardsList", method: "GET" });

angular.module('cardList', []).controller('CardController', function ($scope) {
    $scope.names = [list];
});

2
  • one thing i see, is that the first div should surround the other html elements. The <p> and <ul> tags are not running within angular, because the ng-app attribute is outside these tags Commented Apr 7, 2016 at 12:31
  • You need to use the .then method. have a look at the link Commented Apr 7, 2016 at 12:33

1 Answer 1

3

Try this:

<div ng-app="cardList" ng-controller="CardController">
  <p>
    <input type="text" ng-model="test" />
  </p>

  <ul>
    <li ng-repeat="x in names | filter:test">
      {{x}}
    </li>
  </ul>
</div>

and:

angular.module('cardList', []).controller('CardController', function($scope, $http) {
  $http.get("Controllers/CardController/AllCardsList").then(function(response){ 
    $scope.names = response.data;
  });
});
Sign up to request clarification or add additional context in comments.

8 Comments

i have just tried this, but when i look in the console it gets a 404 error do you know why that could be?
Probably your url is not correct, and MVC cannot find it. You are mapping to a folder structure, rather than using MVC routing. Try "Card/AllCardsList" (assuming your controller is called "CardController" and your action is called "AllCardsList")
you are right the routing is wrong that is why. Hmm it looks like it is calling the model layer and then the controller for some reason
try testing your MVC backend using Postman or even your browser, before testing the angular app. This way you know where the problem is.
i have corrected the routing so that it should work. But i am still getting the 404 not found.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.