sanmoy paul
sanmoy paul

Reputation: 209

$scope is getting lost while opening a modal on ng-click

When I am trying to open a modal on ng-click the scipe variables is getting lost.

var modalInstance = $modal.open({
 templateUrl: 'partials/create.html', 
 controller: 'AppCreateCtrl', 
 scope: $scope // <-- I added this 
});

Upvotes: 0

Views: 231

Answers (1)

nweg
nweg

Reputation: 2865

You should pass data using the resolve property like this:

var modalInstance = $modal.open({
            templateUrl: templateSrv.templateUrl('partials/create.html'),
            controller: modalBootstrap,
            backdrop: 'static',
            keyboard: false,
            resolve: {
                data: function () {
                    var result = {};
                    result.scope = $scope;
                    return result;
                }
            }
        });

modalInstance.result.then(
            function (dataPassedFromModalConroller) {
                //this is called when the modal is closed
            },
            function (dataPassedFromModalConroller) { //Dismiss
              //this is called when the modal is dismissed
            }
        );

Specify the controller for the modal instance like this (we do this in another file):

var modalBootstrap= function ($scope, $modalInstance, data) {
    $scope.modalInstance = $modalInstance;
    $scope.scope = data.userId;
};
modalBootstrap['$inject'] = ['$scope', '$modalInstance', 'data'];

Your modal template would look something like this:

<div ng-controller="AppCreateCtrl">modal content here</div>

Upvotes: 2

Related Questions