【问题标题】:Angular UI Router injector Error in my framework我的框架中的 Angular UI 路由器注入器错误
【发布时间】:2015-10-29 15:49:33
【问题描述】:

我有一个我正在构建的 Angular 框架,我试图让路由器不可知,我希望能够使用任何路由器。到目前为止,我已经用 ng router 对其进行了测试,它工作正常,但是当我尝试使用 UI-Router 时,我得到了注入器错误。我不确定我是否把它放在了错误的模块中,或者它是否是一个更深层次的问题。框架广播一个路由或一个状态,这取决于我如何设置框架指令。我已将 UI-Router 注入到我的主模块中,并将 $stateProvider 注入到需要使用它的控制器中。我难住了。任何帮助将不胜感激。 这是主要模块:

 (function () {
"use strict";

   angular.module("app", ["ptFramework", "ui.router", "ngStorage",    "ui.bootstrap"]);
})();

这是框架模块:

 (function () {
"use strict";
angular.module("ptFramework", [,"ptMenu", "ptDashboard"]);

})();

这是框架控制器:

 (function () {
"use strict";

angular.module("ptFramework").controller("ptFrameworkController",
['$scope', '$window', '$timeout', '$rootScope', '$stateProvider',
    function ($scope, $window, $timeout, $rootScope, $stateProvider) {

        $scope.isMenuVisible = true;
        $scope.isMenuButtonVisible = true;
        $scope.isMenuVertical = true;

        $scope.$on('pt-menu-item-selected-event', function (evt, data) {
            $scope.stateString = data.state;
            $stateProvider.go(data.state);
            checkWidth();
            broadcastMenuState();
        });

        $scope.$on('pt-menu-orientation-changed-event', function (evt, data) {
            $scope.isMenuVertical = data.isMenuVertical;
            $timeout(function () {
                $($window).trigger('resize');
            }, 0);
        });

        $($window).on('resize.ptFramework', function () {
            $scope.$apply(function () {
                checkWidth();
                broadcastMenuState();
            });
        });
        $scope.$on("$destroy", function () {
            $($window).off("resize.ptFramework"); // remove the handler added earlier
        });

        var checkWidth = function () {
            var width = Math.max($($window).width(), $window.innerWidth);
            $scope.isMenuVisible = (width >= 768);
            $scope.isMenuButtonVisible = !$scope.isMenuVisible;
        };

        $scope.menuButtonClicked = function () {
            $scope.isMenuVisible = !$scope.isMenuVisible;
            broadcastMenuState();

        };

        var broadcastMenuState = function () {
            $rootScope.$broadcast('pt-menu-show',
                {
                    show: $scope.isMenuVisible,
                    isVertical: $scope.isMenuVertical,
                    allowHorizontalToggle: !$scope.isMenuButtonVisible
                });
        };

        $timeout(function () {
            checkWidth();
        }, 0);

    }
]);

})();

如您所见,我在 minsafe 数组和函数中都注入了 $stateProvider。我不明白为什么会出现此错误

这是我使用它的 rote.config:

     "use strict";
angular.module('app').config([
    '$stateProvider', function ($stateProvider) {

        $stateProvider
            .state('dashboard',
            {
                url: "/dashboard",
                template: "<h1>dashboard</h1>"
            });

任何意见将不胜感激。

谢谢, 约翰。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    在控制器中:

    $stateProvider.go(data.state);
    

    应该是

    $state.go(data.state);
    

    因此,注入 $state 而不是 $stateProvider

    【讨论】:

    • 谢谢,我一直认为你必须做 $stateProvider.state.go。我显然有我的语法错误。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2018-04-06
    • 2012-12-03
    相关资源
    最近更新 更多