这是我的情况...
我有几个使用 ANGULAR-UI-ROUTE 的路由。
这只是一个例子......
}).state('adminwhiteboard', {//ADMIN DASHBOARD WHITEBOARD
url: "/adminwhiteboard",
views: {
"dashContent": {
templateUrl: "partials/administration/whiteboard/admin-whiteboard-plan-content.html",
data: {
authorization: true, //Authorization REQUIRED
redirectTo: 'login', //Go Back to LOGIN PAGE
memory: true, //Remember that this user tried!!!
pageName: "adminwhiteboard",
members: [10, 20, 50, 60]
},
controller: function ($scope, $stateParams, $document, $location) {
if ($scope.$parent.pagename !== "adminwhiteboard") {
$scope.$parent.pagename = "adminwhiteboard";
console.log("$STATE PARAMS: ", $stateParams);
console.log("$STATE LOC: ", $location);
console.log("$STATE LOC: ", $document);
//Remove ALL the "IN" for collapse and add back only
//for current dropdown
$("#mainNavigator li > a").removeClass("in");
$("#dropMenu1 li > a").addClass("in");
}
}
}
},
resolve: {
id: ['$stateParams', function ($stateParams) {
console.log("State PageName: ", $stateParams.name);
return $stateParams.id; //By putting this here...
}]
}
各部分如下:
data: {
authorization: true, //Authorization REQUIRED
redirectTo: 'login', //Go Back to LOGIN PAGE
memory: true, //Remember that this user tried!!!
pageName: "adminwhiteboard",
members: [10, 20, 50, 60]
}
我希望能够访问 data.authorization、data.redirectTo、data.memory 等...
从我的 Controller 内部(不是下面的 Controller )
controller: function ($scope, $stateParams, $document, $location) {
if ($scope.$parent.pagename !== "adminwhiteboard") {
$scope.$parent.pagename = "adminwhiteboard";
console.log("$STATE PARAMS: ", $stateParams);
console.log("$STATE LOC: ", $location);
console.log("$STATE LOC: ", $document);
//Remove ALL the "IN" for collapse and add back only
//for current dropdown
$("#mainNavigator li > a").removeClass("in");
$("#dropMenu1 li > a").addClass("in");
}
}
}
我所说的 Controller 是另一个文件,它是整个系统的主 Controller 。当然还有更多,但我只是想从我的 Controller 访问数据中的项目,如下所示:
$scope.state.currpage,等等,等等,等等......明白我的意思了吗?
顺便说一句,每次用户单击 ui-sref 以更改 route 的部分内容时,都会设置 currpage。
谢谢大家。
更新:
回答JC...
我已经在这样做了。所以我只需要扩展它?
$scope.changeRoute = function (url) {
//console.log($scope);
$location.path(url);
$timeout(function () {
//outside of the digest, by using $timeout(fn, 0, false), where the
//false value tells Angular not to wrap this fn in a $apply block:
$scope.$apply();
}, 0, false);
//ROUTE for Change Start and Change Success
$rootScope.$on("$routeChangeStart",
function (event, current, previous, rejection) {
console.log($scope, $rootScope, $route, $location);
});
$rootScope.$on("$routeChangeSuccess",
function (event, current, previous, rejection) {
console.log($scope, $rootScope, $route, $location);
});
};
最佳答案
您可以在 Controller 中设置状态更改事件处理程序,并通过它们访问数据。
//UI-Router 1.x
$transitions.onSuccess(function(){})
//UI-Router 0.4.x (legacy)
$rootScope.$on('$stateChangeSuccess', function(){});
关于javascript - 如何从 Controller 中访问 app.js 中的 .state() 以使用 ROUTER 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369864/