1

So I have been able to confirm that data is getting into my angular function, but have been banging my head against the wall trying to get it to the DOM. I am doing everything as simple as I can before I start creating my full crud.

I'm sure I am missing something simple, as I am a newb with Angular and most certainly utilizing $http through asp.net api.

(function () {
angular
    .module('appmodule')
    .controller('campaignservice', ['$scope', '$http', campaignservice])

function campaignservice($scope, $http) {
    $http.get("http://localhost:58291/api/campaigns").then(function (data) {
        $scope.campaigns = data;
    });
  };
})();

and then the html

<div>
<div ng-controller="campaignservice">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Pages </th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="campaign in campaigns">
                <td>{{campaign.Id}}</td>
                <td>{{campaign.Name}}</td>
                <td>{{campaign.Pages}}</td>
            </tr>
        </tbody>
    </table>
</div>

Whats my deal?? My brain is bleeding.

21
  • 1
    There might be an error that you are not catching, extend your code with this: .then(function (data){ ... }, function(error){ ... }); Commented Feb 15, 2018 at 14:40
  • 1
    @Zooly that's a controller with a proper [ ] notation Commented Feb 15, 2018 at 14:47
  • 2
    Does then() return the data? I thought it returned a response and the data is on response.data? Commented Feb 15, 2018 at 14:54
  • 1
    When I look at examples online success() returns just data, but then() returns response.data from what I'm seeing. yes, there is data but the data you want I think is response.data (or data.data in your case) Commented Feb 15, 2018 at 14:57
  • 1
    @JeffLongo consider $scope.campaigns = data.data; or look at it with console.log(data); Commented Feb 15, 2018 at 14:59

1 Answer 1

1

From help in the comments I merely needed to make the following amendment.

$scope.campaigns = data.data;
Sign up to request clarification or add additional context in comments.

2 Comments

My only suggestion would be to rename the parameter passed from data to response so you have response.data. You are getting a response object back from the then() function. Read your backstory, keep sticking with it! A shame the math scared you back then. Universities for some reason think heavy math is required for programming when 90% of the time it's just not. Good luck man!
thanks...will do! response.data is a tad more readable than data.data hahah

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.