在控制器中,我已经从使用$scope对象转换为vm = this。我知道您必须在controllerAs或view中声明一个state。
我有两个使用ng-repeat的表,第一个很好,所有的数据都被填充,但是第二个表由于某种原因没有被填充。我已经玩了一遍,用户单击的项被记录在控制台中,但是第二个表没有填充数据。和$scope合作得很好。
知道我哪里出问题了吗?
var app = angular.module('myApp', ['ngRoute', 'ui.router']);
app.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', '$routeProvider', function($stateProvider, $locationProvider, $urlRouterProvider, $routeProvider) {
$urlRouterProvider
.when('/', '/patents/')
.otherwise('/patents/');
$stateProvider
.state("patents", {
url: "/patents",
templateUrl: "templates/patents/list/list-patents.htm",
controller: "patentCtrl",
controllerAs: "patents"
})
.state("patents.item", {
url: "/:id",
templateUrl: "templates/patents/list/patent-item.htm",
controller: "patentCtrl",
controllerAs: "item"
})
}]);
app.factory('patentsService', function($http, $q) {
var factory = {};
var REST_SERVICE_URI = 'http://localhost:8080/Sprint002b/restpatent/';
factory.fetchAllPatents = function() {
var deferred = $q.defer();
$http.get(REST_SERVICE_URI)
.then(
function (response) {
deferred.resolve(response.data);
},
function(errResponse){
console.error('Error while fetching Users');
deferred.reject(errResponse);
}
);
return deferred.promise;
}
factory.select = function(item) {
selectedItem = item;
return selectedItem;
}
return factory;
});
app.controller('patentCtrl', ['patentsService', function(patentsService) {
var vm = this;
vm.patent={id:null, patentApplicationNumber:'', clientRef: '', renewalStatus: '', currentRenewalCost: '', costBandEndDate:'', renewalCostNextStage:'', renewalDueDate:''};
vm.patents=[];
vm.select = function(item) {
vm.patentItem = patentsService.select(item); //THIS SHOULD POPULATE TABLE
console.log(vm.patentItem)
}
vm.fetchAllPatents = function(){
patentsService.fetchAllPatents()
.then(
function(d) {
vm.patents = d;
},
function(errResponse){
console.error('Error while fetching Users');
}
);
}
vm.fetchAllPatents();
}]);工作台
<tbody>
<tr ng-repeat="x in patents.patents">
<td ng-click="patents.select(x)"><a ui-sref="patents.item({id: x.id})">{{x.applicationNumber}}</a></td>
<td ng-bind="x.clientRef"></td>
<td ng-bind="x.currentRenewalCost">$</td>
<td ng-bind="x.costBandEndDate"></td>
<td ng-bind="x.renewalCostNextStage"></td>
<td ng-bind="x.renewalDueDate"></td>
</tr>
</tbody>破台
<tbody>
<tr ng-repeat="x in item.patentItem">
<td>{{x.applicationNumber}}</td>
<td>{{x.clientRef}}</td>
<td>{{x.renewalStatus}}</td>
<td>{{x.costBandEndDate}}</td>
<td>{{x.renewalCostNextStage}}</td>
<td>{{x.renewalDueDate}}</td>
</tr>
</tbody>发布于 2017-06-23 09:59:52
然后,您应该为patents.item状态创建一个新的单独控制器。在加载时,您将只从patentsService中检索当前选定的项。
//config
.state("patents.item", {
url: "/:id",
templateUrl: "templates/patents/list/patent-item.htm",
controller: "patentItem", //<-- controller added here
controllerAs: "item"
})
//patenItem new controller
.controller('patentItem', ['patentsService', function(patentsService) {
var item = this;
item.select = function(items) {
vm.patentItem = patentsService.select(item);
console.log(vm.patentItem)
};
function init(){
item.select();
}
init();
}]);https://stackoverflow.com/questions/44718234
复制相似问题