user672009
user672009

Reputation: 4585

Can't print simple value from rest controller

I have rest api which returns the total number of expenses in a given quarter.

I've tried to create a service which should performe the request.

app.factory('ExpenseTotal', function($resource) {
    return $resource('api/expense/quarter/:q', {q: '@q'});
});

I'm trying to use that service in my controller. Like so:

app.controller('DashboardController', function($scope, ExpenseTotal) {
    $scope.updateTotalExpenses = function() {
        $scope.totalExpenses = ExpenseTotal.get({ q: $scope.selectedItem });
    }
});

My view looks like the following:

Dashboard!
<label>Select quarter:
    <select ng-model="selectedItem" ng-change="updateTotalExpenses()">
        <option value="1">1st Quarter</option>
        <option value="2">2nd Quarter</option>
        <option value="3">3rd Quarter</option>
        <option value="4">4th Quarter</option>
    </select>
</label>
<div>Total expenses: {{totalExpenses}}</div>

However the only thing which gets printed is: {}

Anyone got a clue about what I'm doing wrong?

The complete code can be found at: https://github.com/tonsV2/MyBiz/

Upvotes: 0

Views: 34

Answers (1)

Meir
Meir

Reputation: 14395

The $resource returns an object that has a promise, once it is resolved you have the value. Change your code to:

ExpenseTotal.get({ q: $scope.selectedItem }).$promise.then(function(result){
  $scope.totalExpenses = result;
});

Upvotes: 1

Related Questions