我試圖讓ui路由器的決心將其值傳遞給控制器portalsForUserCtrl
。角狀態解決不注入控制器
這裏是路由器:
(function() {
'use strict';
var myApp = angular.module("myApp", ["common.services", "ui.router", 'ngMessages']);
myApp.config(["$stateProvider", "$urlRouterProvider",
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state("portalsForUser", {
url: "/userPortal/portalsForUser/:id",
templateUrl: "app/userPortal/portalsForUser.html",
controller: "portalsForUserCtrl as vm",
resolve: {
userPortalService: "userPortalService",
portalsForUser: function (userPortalService, $stateParams) {
var userId = $stateParams.id;
console.log(userId); //shows userId correctly
return userPortalService.getPortalsForUserPromise(userId)
.then(function (response) {
var userPortals = response.data;
console.log("userPortals", userPortals); //shows portals
return userPortals;
});
}
}
})
}]
);
這裏是整個控制器:
(function() {
"use strict";
angular.module("myApp")
.controller("portalsForUserCtrl", portalsForUserCtrl);
portalsForUserCtrl.$inject = ['portalsForUser', 'userPortalService'];
function portalsForUserCtrl(portalsForUser, userPortalService) {
console.log("in portalsForUserCtrl");
var vm = this;
vm.portalsForUser = portalsForUser;
console.log(portalsForUser);
}
}());
在mainCtrl,其是用於index.html中的控制器,I撥打:
$state.go("portalsForUser", ({ "id": userId }));
以下是查看應用程序/ userPortal/portalsForUser.html的代碼:
<div class="container">
<table class="table table-condensed table-striped table-bordered">
<tbody>
<tr>
<th class="col-md-2"> </th>
<th class="col-md-4">
Portal Name
</th>
</tr>
<tr ng-repeat="userPortal in vm.portalsForUser">
<td>
{{userPortal.portal.portalName}}
</td>
<td class="">
<a class="btn btn-primary" ui-sref="goSomewhere({id: userPortal.portal.id})">
Go
</a>
</td>
</tr>
</tbody>
</table>
下面是userPortalService代碼:
(function() {
"use strict";
angular.module("myApp")
.service('userPortalService', userPortalService);
userPortalService.$inject = ['userPortalResource', '$http', 'appSettings']
function userPortalService(userPortalResource, $http, appSettings) {
var getPortalsForUserPromise = function (id) {
return $http.get(appSettings.serverPath + '/api/UserPortal/GetPortalsForUser/' + id);
};
return {
getPortalsForUserPromise: getPortalsForUserPromise
};
}
}());
的網址更改爲正確的/userPortal/portalsForUser/:id
但portalsForUserCtrl
功能不火。 只有當我在同一個url上輸入enter時,portalsForUserCtrl被實例化並且數據出現在視圖中。我錯過了什麼?
在控制檯中的任何錯誤?我也看不到你的解決方案中需要'userPortalService:「userPortalService」 – Phil
控制檯沒有錯誤。刪除你提到的行沒有任何影響,所以你是正確的,這是不需要的,謝謝但是,這種改變並不能解決我的問題。 – Dewey