1

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?

1 Answer 1

1

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;
            });
        }]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Muhammad, although $scope.Costs does contain the data object array, the view still doesn't get updated, or it half updated - the table repeat now has two rows but has not binded on {{Resource.CostType}} or the number field...

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.