简体   繁体   中英

How to pass data into an angular.js UI bootstrap modal controller

Using Angular.js UI bootstrap modal, how can I pass data into a modal popup's controller? I am currently trying:

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

The controller confirmationModal is:

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

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

But this errors with:

Unknown provider: fooProvider

You can try to define the "confirmationModal" controller with angular.module('app').controller(...) instead.

If you want to use a string to refer to a controller, you need to register it to an angular module.

So, you have two ways to make it work:

1.Use string

In modal settings:

controller: "confirmationModal"

In controller definition (assume "app" is your module name):

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

2.Use function itself

In modal settings:

controller: confirmationModal

In controller definition:

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

Can you tell the difference?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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