【问题标题】:Use a provider to run a command onEntet使用提供程序运行命令 onEntet
【发布时间】:2017-02-13 07:19:12
【问题描述】:

我正在使用 ui-router 来更改 AngularJS 应用程序中的状态。我有一个提供程序,它使用 JavaScript 在存在 div 的 HTML 中创建图形。只有一个视图具有我的图形提供程序使用的 div 元素,因此我想在进入该状态时调用 graphProvider.drawGraph()。

我正在努力绘制图表:我得到的错误是,“未捕获的错误:[$injector:modulerr] 无法实例化模块 dataPortalApp,原因是: 错误:[$injector:modulerr] 无法实例化模块 graphProvider 由于: 错误:[$injector:nomod] 模块“graphProvider”不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。”

我的 router.js 看起来像:

(function() {
  angular.module('dataPortalApp')
    .config(DataPortalRouter);

  DataPortalRouter.$inject = ['$stateProvider', '$urlRouterProvider', '$locationProvider', 'graphProvider'];

  function DataPortalRouter($stateProvider, $urlRouterProvider, $locationProvider, graphProvider) {
    $urlRouterProvider.otherwise("/");
$stateProvider
  .state('dashboard', {
    url: '/dashboard',
    templateUrl: '/views/_dashboard.html',
    onEnter: function() {
      graphProvider.drawGraph()
    }
  })
  .state('index', {
    url: '/',
    templateUrl: '/views/_index.html'
  });

  }//end DataPortalRouter

})();

我的应用,js 看起来像:

(function(){
  angular.module('dataPortalApp', ['ui.router', 'graphProvider']);
})();

非常感谢任何帮助!

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    确保您的所有模块脚本都以正确的顺序导入。 新的顺序应该是:

    1. angular.js
    2. angular-ui-router.js
    3. app.js
    4. graphProvider.js(在 app.js 之后,因为它是您模块的一部分,而不是我最初认为的另一个模块。)

    并删除graphProvider的模块依赖:

    App.js:

    (function(){
      angular.module('dataPortalApp', ['ui.router']);
    })();
    

    【讨论】:

    • 感谢您的帮助!现在我得到Uncaught Error: [$injector:nomod] Module 'dataPortalApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 我似乎无法获胜 - 如果我把应用程序放在首位,它需要提供者。如果我把提供者放在第一位,它会说应用程序不可用。更多代码在这里github.com/tmurphree/projectFilter/tree/dev如果你有机会看看。
    • @tmurphree 我已经更正了我原来的答案。查看您的完整代码,graphProvider.js 应该在 app.js 之后,因为它声明了您将 graphProvider 附加到的模块(我的错误很抱歉)。回到最初的错误,我认为这只是因为您试图将提供程序作为模块注入并且它抱怨因为它不是模块,所以只需从 app.js 中删除它。
    • 你是男人!这完全解决了它!很抱歉延迟回复。
    • 非常好,如果有帮助,请标记为答案。谢谢
    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多