我有休息api,它返回给定季度的总费用。
我试图创建一个应该执行请求的服务。
app.factory('ExpenseTotal', function($resource) {
return $resource('api/expense/quarter/:q', {q: '@q'});
});
我正在尝试在我的控制器中使用该服务。像这样:
app.controller('DashboardController', function($scope, ExpenseTotal) {
$scope.updateTotalExpenses = function() {
$scope.totalExpenses = ExpenseTotal.get({ q: $scope.selectedItem });
}
});
我的观点如下:
仪表板!<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>
然而,打印的唯一内容是:{}
任何人都知道我做错了什么?
完整的代码可在以下网址找到:https://github.com/tonsV2/MyBiz/
答案 0 :(得分:1)
$ resource返回一个具有promise的对象,一旦解析就有了值。将您的代码更改为:
ExpenseTotal.get({ q: $scope.selectedItem }).$promise.then(function(result){
$scope.totalExpenses = result;
});