gpt4 book ai didi

javascript - Angular 状态解析未注入(inject) Controller

转载 作者:行者123 更新时间:2023-12-03 06:04:08 25 4
gpt4 key购买 nike

我正在尝试获取 ui-router 的解析,以将其值传递给 Controller ​​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;
});
}
}
})
}]
);

这是整个 Controller :

(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 的 Controller )中,我调用:

$state.go("portalsForUser", ({ "id": userId }));

以下是 View app/userPortal/portalsForUser.html 的代码:

<div class="container">
<table class="table table-condensed table-striped table-bordered">
<tbody>
<tr>
<th class="col-md-2">&nbsp;</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 才会被实例化并且数据会出现在 View 中。我错过了什么?

最佳答案

$state.go 语句中存在语法错误。

更改此:

$state.go("portalsForUser", ({ "id": userId }));

对此:

$state.go("portalsForUser", { "id": userId });

关于javascript - Angular 状态解析未注入(inject) Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com