如何从控制器中的app.js访问.state()以使用来自ROUTER的项目

时间:2017-09-22 17:07:47

标签: javascript angularjs angular-ui-router

这是我的情况......

我有几个ROUTES使用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: 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");
                    }
                }
            }

我所说的控制器是另一个文件,它是整个系统的主控制器。当然还有更多,但我只是想访问数据中的项目:来自我的控制器,如:

$ scope.state.currpage,blah,blah,blah ....来找我?

顺便说一下,每当用户点击ui-sref来改变路线中的部分时,就会设置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);
                        });
            };

1 个答案:

答案 0 :(得分:1)

您可以在控制器中设置状态更改事件处理程序,并通过它们访问数据。

//UI-Router 1.x
$transitions.onSuccess(function(){})

//UI-Router 0.4.x (legacy)
$rootScope.$on('$stateChangeSuccess', function(){});