具有angular-ui-router的第一个角度分量。页面加载但没有数据。服务中的Console.log .then显示数据是一个包含21个元素的数组。看起来很简单,把我的头发撕掉了 -
stateProvider:
$stateProvider
.state('home', {
template: '<prospects-home resource-data ="$resolve.resourceData"></prospects-home>',
resolve: {
resourceData: function (dataService) {
return dataService.getResources();
}
}
})
组件:
angular
.module("myApp")
.component('prospectsHome', {
templateUrl: '/Scripts/partials/partial-home.html',
bindings: {
resourceData: '=',
},
controller: function() {
var $ctrl = this;
$ctrl.resources = resourceData;
}
})
模板:
<table>
<tbody>
<tr ng-repeat="resource in $ctrl.resources">
<td>{{ resource.firstName }}, {{ resource.lastName }}</td>
</tr>
</tbody>
</table>
服务:
var app = angular.module("myApp");
app.service('dataService', ['$http', '$q', function ($http, $q) {
return {
getResources: function () {
var deferred = $q.defer();
return $http.get('/Scripts/data/resources.json')
.then(function (response) {
console.log(response.data);
deferred.resolve(response.data);
});
return deferred.promise;
}
}
}]);