Javascript angularjs ng重复资源中的数据未显示
我有一个简单的AngularJS应用程序,它使用$resource获取RESTful API数据,问题是一旦数据最终到达并分配给我的$scope,视图就不会用数据更新 很可能我的代码中有错误,因为我是AngularJS的新手 我的服务:Javascript angularjs ng重复资源中的数据未显示,javascript,json,angularjs,angularjs-ng-repeat,angular-resource,Javascript,Json,Angularjs,Angularjs Ng Repeat,Angular Resource,我有一个简单的AngularJS应用程序,它使用$resource获取RESTful API数据,问题是一旦数据最终到达并分配给我的$scope,视图就不会用数据更新 很可能我的代码中有错误,因为我是AngularJS的新手 我的服务: (function () { 'use strict'; var taxYearApp = angular.module('taxYearApp'); taxYearApp.factory('costService', ['$reso
(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 });
}
};
}
]);
})();
这是我的控制器:
(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];
});
}]);
})();
我在这里尝试了一些不同的方法,但似乎没有任何效果,最初只是$scope.Costs=costService.getCosts(taxYearID)代码>
现在我至少可以看到,$scope.Costs
实际上包含了我想要的数据数组,只是我的视图并没有被刷新
说到这里,我的观点是:
<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>
有人能建议我做错了什么,或者如何用我的异步数据刷新$scope吗?您应该将$scope.Costs
分配给中从承诺返回的数据。然后()
感谢Muhammad,虽然$scope.Costs
确实包含数据对象数组,但视图仍然没有更新,或者它已经更新了一半-表repeat现在有两行,但没有绑定到{Resource.CostType}
或数字字段上。。。
[
{
"CostID": 1,
"CostTitle": "Wage",
"GrossAmount": 10001,
"IsReadOnly": false
},
{
"CostID": 2,
"CostTitle": "Total Expenses",
"GrossAmount": 3000,
"IsReadOnly": false
}
]
taxYearApp.controller('costController', ['$scope', 'costService',
function ($scope, costService) {
...
promise.$promise.then(function (data) {
$scope.Costs = data;
});
}]);