【问题标题】:How to inject $dialog into existing angularjs page controller如何将 $dialog 注入现有的 angularjs 页面控制器
【发布时间】:2015-07-14 15:24:48
【问题描述】:

我想使用 bootstrap-ui 的 $dialog 指令在 AngularJS 中创建简单的弹出对话框。

当我尝试注入我的控制器时,我得到 $dialog 未定义。有人可以在以下设计中提供“如何正确注入 $dialog”的提示并调用它来创建弹出对话框吗?

提前致谢

index.js:

angular.module('myapp', ['myapp.core','myapp.templates','ui.router', 'ui.bootstrap',
  'angularChart', 'angularjs-dropdown-multiselect', 'smart-table', 'angularModalService']);

页面控制器:

(function() {
  'use strict';
  angular.module('myapp').controller('Page3Controller', Page3Controller);
  function Page3Controller(
    $scope,
    $dialog, // undefined
    Page3Service,
    Utility) {

【问题讨论】:

  • $modal,不是$dialog

标签: javascript angularjs twitter-bootstrap


【解决方案1】:

正如this post 的公认答案所说, $dialog 服务被重构为 $modal 版本 0.6.0ui-bootstrap$dialog 的功能应该仍然可用,只是通过 $modal 代替。

在您的模块中注入$modal 服务,它应该可以工作。阅读docs

所以,要编辑你的代码,

 (function() {
      'use strict';
      angular.module('myapp').controller('Page3Controller', Page3Controller);
      function Page3Controller(
        $scope,
        $modal, // now do with it whatever you want, refer to the docs for detailed change
        Page3Service,
        Utility) {

请检查它是否以这种方式工作。

【讨论】:

  • 我可以注入 $modal,但是当我尝试遵循文档中的建议时,我收到错误消息,指出未定义“ModalInstanceCtrl”。我将它放在一个单独的 .js 文件 instanceControl.js 文件中。
  • 您应该导入包含第二个 cotnroller 的文件(例如通过 index.html 中的
【解决方案2】:

控制器:

(function(){
    angular.module("myApp").controller("Page3Controller", ["$scope", 
        "$dialog", "Page3Service", "Utility", Page3Controller]);

        function Page3Controller($scope, $dialog, Page3Service, Utility){
            $dialog.whatEverYouNeedToDo();

        }
});

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    相关资源
    最近更新 更多