简体   繁体   English

如何在我的应用程序中包含和使用ui-router而不会出现注射器错误?

[英]How to include and use ui-router in my app without injector errors?

UPDATE I've reverted back to a state before including any ui-router 更新我已经恢复到包含任何ui路由器之前的状态


I was getting that terrible $injector error that I could not solve, I've since reverted back and need to know how to simply include ui-router and add in a Config function that will simply route the user to /login which loads /login.html 我遇到了我无法解决的可怕的$ injector错误,此后我又退回去,需要知道如何简单地包括ui-router并添加一个Config函数,该函数将用户简单地路由到/ login,从而加载/ login html的

I've done this many times before, just this time it won't work. 我之前已经做过很多次,只是这次不起作用。

(function() { "use strict";

    var app = angular.module('tickertags', [
        'infinite-scroll',
        'apiFactory',
        'scopeFactory',
        'tagFactory',
        'tickerFactory',
        'timeSpanFactory',
        'overlayDirective',
        'searchPopoverDirectives',
        'notificationDirectives',
        'platformHeaderDirectives',
        'controlHeaderDirectives',
        'viewHeaderDirectives',
        'alertsPanelController',
        'alertPanelDirectives',
        'tickersPanelDirectives',
        'tickersSelectFactory',
        'tagsPanelDirectives',
        'tagDetailsFactory',
        'tagHoverDirective',
        'tagSearchDirective',
        'tagsFilterDirective',
        'focusMeDirective',
        'chartHeaderController',
        'chartHeaderDirectives',
        'chartPanelDirective',
        'chartIQDirective',
        'socialMediaController',
        'socialMediaDirectives',
        'socialMediaFactory'
    ])
    .controller('DashCtrl', Controller);

    /*
        I WANT TO ADD THE CONFIG HERE...
        /login = login.html
    */

    Controller.$inject = [
        '$scope',
        '$rootScope',
        'ScopeFactory',
        'TickerFactory'];

    function Controller($scope,
                        $rootScope,
                        ScopeFactory,
                        TickerFactory) {

        /** Init DashCtrl scope */
        /** ----------------------------------------------------------------- */
        var vs          = $scope;
            vs.showNote = true,
            vs.mouseX   = '',
            vs.mouseY   = '';
            ScopeFactory.saveScope('root', vs);

        /** Detect if a tagHover has been shown and ready bodyClick to close */
        vs.$on('tagHoverOpen', function(events,data) {
            vs.bodyClick = function() {
                $rootScope.$broadcast('bodyClick');
            };
        });

        /** Remove eventListener after tagHover is closed */
        vs.$on('destroy', function() {
            vs.bodyClick = angular.noop();
        });

        /** Get and store all tickers, eventually just first 100 */
        getAllTickers();

        function getAllTickers() {
            TickerFactory.getTickers();
        };

    };

})();

You need to add ui.router module inside your app module to use angular ui.router features. 您需要在应用程序模块内部添加ui.router模块,才能使用ui.router角度功能。

Code

var app = angular.module('tickertags', [
    //..other modules here.
    'socialMediaFactory',
    'ui.router' //<--added it
])

If you have created the framework for your project using yeoman (which is always a good idea to bootstrap your project and avoid writing lot of boilerplate code), then you would have an existing bower and grunt configuration in your project. 如果您使用yeoman为项目创建了框架(引导项目并避免编写大量样板代码始终是一个好主意),那么您的项目中将具有现有的Bower和Grunt配置。

From there on, these are the steps you can follow for replacing ngRouter in your project with ui-router 从那里开始,您可以按照以下步骤用ui-router替换项目中的ngRouter

  1. Execute bower install angular-ui-router --save 执行bower install angular-ui-router --save

  2. Modify your index.html by replacing 通过替换来修改index.html

<div ng-view=""></div> with <div ui-view=""></div>

  1. Update your app.js to include the ui-router module and the appropriate configuration for different states 更新您的app.js以包括ui-router模块和针对不同状态的适当配置

Here's a sample configuration: 这是一个示例配置:

angular
  .module('myApp', [
    'ui.router'
  ])
  .config(function ($stateProvider, $urlRouterProvider) {
    // For any unmatched url, send to /
    $urlRouterProvider.otherwise("/");

    $stateProvider
      .state('/', {
        url:"/",
        templateUrl: '../modules/main/main.html',
        controller: 'mainCtrl'
      })
      .state('about', {
        url:"/about",
        templateUrl: '../modules/about/about.html',
        controller: 'aboutCtrl'
      });
  });

If you have grunt and bower configured for your project then the dependent angular-ui-router.js will be added automatically to your index.html 如果您为项目配置了grunt和bower,则相关的angular-ui-router.js将自动添加到index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM