【问题标题】:How to pass data into an angular.js UI bootstrap modal controller如何将数据传递到 angular.js UI 引导模式控制器
【发布时间】:2014-08-28 22:22:30
【问题描述】:

使用 Angular.js UI bootstrap 模态,我如何将数据传递到模态弹出窗口的控制器中?我目前正在尝试:

var modalInstance = $modal.open({
    templateUrl: 'partials/confirmation-modal.html',
    controller: 'confirmationModal',
    resolve: {
        foo: function() { return 'bar'; }
    },
    backdrop: 'static',
    keyboard: true
});

控制器confirmationModal是:

    (function(_name) {
        /**
         * @ngInject
         */
        function _controller($scope, foo) {
            console.log(foo);  
        }

        angular
            .module('myApp')
            .controller(_name, _controller);
    })('confirmationModal');

但是这个错误:

Unknown provider: fooProvider

【问题讨论】:

    标签: angularjs angularjs-scope angular-ui angular-ui-bootstrap


    【解决方案1】:

    您可以尝试使用angular.module('app').controller(...) 来定义“confirmationModal”控制器。

    如果要使用字符串来引用控制器,则需要将其注册到 Angular 模块中。

    所以,你有两种方法可以让它发挥作用:

    1.使用字符串

    在模态设置中:

    controller: "confirmationModal"
    

    在控制器定义中(假设“app”是您的模块名称):

    angular.module('app').controller("confirmationModal", function($scope, foo) {
        console.log(foo);
    });
    

    2.使用函数本身

    在模态设置中:

    controller: confirmationModal
    

    在控制器定义中:

    var confirmationModal = function($scope, foo) {
        console.log(foo);
    }
    

    你能分辨出来吗?

    【讨论】:

    • 当我尝试时:controller: confirmationModal 我得到:confirmationModal is not defined
    • 发现了我的问题。我将控制器设置为在模式选项和模板本身中使用ng-controller。这真的很难发现。
    猜你喜欢
    • 2021-02-19
    • 2020-02-24
    • 2015-10-24
    • 2018-01-25
    • 1970-01-01
    • 2012-08-01
    • 2013-07-21
    • 2021-09-30
    • 2019-04-26
    相关资源
    最近更新 更多