Steve Westwood
Steve Westwood

Reputation: 131

angularjs ng-repeat data from resource not displaying

I have a simple AngularJS app which gets RESTful API data using $resource, trouble is the view is not updated with the data once it finally arrives and is assigned to my $scope.

It's quite possible there may be an error in my code since I'm new to AngularJS!

My service:

(function () {
    'use strict';

    var taxYearApp = angular.module('taxYearApp');

    taxYearApp.factory('costService', ['$resource', 
        function ($resource) {
            var theUrl = 'http://localhost/taxcalculator/api/CostApi/';
            var CostResource = $resource(theUrl + ':taxYearID', { taxYearID: 'taxYearID' }, { 'update': { method: 'PUT' } });
            return {
                getCosts: function (taxYearID) {
                    return CostResource.query({ taxYearID: taxYearID });
                }
            };
        }
    ]);
})();

This is my controller:

(function () {
    "use strict";

    var taxYearApp = angular.module('taxYearApp');

    taxYearApp.controller('costController', ['$scope', 'costService',
            function ($scope, costService) {
                $scope.Costs = [];
                var taxYearID = 1;
                var promise = costService.getCosts(taxYearID);
                promise.$promise.then(function () {
                    $scope.Costs = [promise];
                });
            }]);
})();

I've tried a few different things here but nothing seems to work, initially it was just $scope.Costs = costService.getCosts(taxYearID);.

Now I can at least see that $scope.Costs actually does contain an array of the data I want in there, it's just my view is not being refreshed.

Speaking of which, here is my view:

    <div ng-controller='costController'>
        <div ng-repeat="Resource in Costs">
            <form name='item_{{$index}}_form' novalidate>
                <table>
                    <tr>
                        <td><h3>{{Resource.CostType}}</h3></td>
                        <td><input type="number" ng-model="Resource.CostAmount" required /></td>
                    </tr>
                </table>
            </form>
        </div>
    </div>

I changed the object to 'Resource' here as that is how the promise seems to format the JSON.

If I request the webAPI manually this is the JSON I get back:

[
    {
        "CostID": 1,
        "CostTitle": "Wage",
        "GrossAmount": 10001,
        "IsReadOnly": false
    },
    {
        "CostID": 2,
        "CostTitle": "Total Expenses",
        "GrossAmount": 3000,
        "IsReadOnly": false
    }
]

Can anyone suggest either what I am doing wrong or how to refresh the $scope with my async data?

Upvotes: 1

Views: 1074

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27023

You should assign $scope.Costs to the data returned from the promise in .then()

taxYearApp.controller('costController', ['$scope', 'costService',
        function ($scope, costService) {
            ...
            promise.$promise.then(function (data) {
                $scope.Costs = data;
            });
        }]);

Upvotes: 1

Related Questions