【问题标题】:how to inject module into config?如何将模块注入配置?
【发布时间】:2014-01-21 18:38:06
【问题描述】:

您好,我发现代码示例显示了如何在特定状态下打开模式窗口: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-open-a-dialogmodal-at-a-certain-state

但我不知道如何将 $modal 和 $scope 注入:

angular.module('my-module',['ui-router']).config(function($stateProvider, $modal, $scope))

这不起作用,我还读到: how to inject dependency into module.config(configFn) in angular

所以我知道我们只能注入提供程序和常量。但是如何运行该示例:

`$stateProvider.state("items.add", {
url: "/add",
onEnter: function($stateParams, $state, $modal, $resource) {
    $modal.open({
        templateUrl: "items/add",
        resolve: {
          item: function() { new Item(123).get(); }
        },
        controller: ['$scope', 'item', function($scope, item) {
          $scope.dismiss = function() {
            $scope.$dismiss();
          };

          $scope.save = function() {
            item.update().then(function() {
              $scope.$close(true);
            });
          };
        }]
    }).result.then(function(result) {
        if (result) {
            return $state.transitionTo("items");
        }
    });
}`

附言 我的目标是在用户更改 url 地址时打开新的模块窗口..

【问题讨论】:

  • 我注意到你正在使用 ui-router,但你还没有将 ui.router 注入到你的模块依赖中......
  • 这只是来自 angular-ui 常见问题的示例。我没有遇到问题,但我只需要在用户更改 url 时打开模式窗口。例如,当用户添加登录时,我只打开新的模式窗口......就是这样。

标签: angularjs angular-ui angular-ui-router


【解决方案1】:

试试

angular.module('my-module',[]).config(['@stateProvider','$module','$scope', function(@stateProvider, $module, $scope))

我不太确定 $module 是什么?那是你创建的一些 Angular 服务吗?

【讨论】:

  • 对不起,来自 ui-bootstrap 的 $modal 不是 $module 我刚刚修复 :) 谢谢我尝试
  • 不,它不工作我有错误:错误:[$injector:modulerr] 无法实例化模块 turnigo-core-app 由于:[$injector:unpr] 未知提供者:$modal跨度>
  • 您是否在索引文件中包含了 ui-router 的参考 javascript 文件?
  • 是的,我当然没有遇到 ui-router 和其他东西的问题,它工作正常。但现在这是另一个问题,因为 $modal 不是角度提供者。但是为什么他们用它来展示例子
  • 好吧,只是检查一下,这是我通常犯的错误!我不完全确定您可以将 $scope 注入 .config。你确定你需要吗?在那个例子中,你给它似乎不像注入 $modal 或 $scope 到配置函数中
猜你喜欢
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多