0

I am opening a uibModal each time a page is opened like:

genres.js

        app.controller('genresController', ['$scope', '$http', '$uibModal', 'blockUI', 'notifyService', 'lib',
function ($scope, $http, $uibModal,blockUI, notifyService, lib) {
        $scope.genres = [];
        var init = function () {
            //Initial code
            loadGenres();
        };


        var loadGenres = function () {

            /*... some non-important codes... */

            openFavoriteGenreDialog();

            /*... some non-important codes... */

            }, lib.handleError);
        };

        var openFavoriteGenreDialog = function () {
            var modalInstance = $uibModal.open({
                templateUrl: '/Dialog/FavoriteGenre',
                controller: 'favoriteGenreDialogController'
            });
            modalInstance.result.then(function (msg) {


            }, function (msg) {
                //dismiss callback
            });
        };

        init();
}]);

and the modal controller is in another file and the html of that modal has a cancel button.

FavoriteGenre.cshtml

        <button class="btn btn-default" type="button" ng-click="cancel()">Cancel</button>

That cancel button is beeing treated in another controller that has the function that so far looks like it

favGenrer.js

    $scope.cancel = function () {
        var modalInstance = $uibModal.open({
            templateUrl: '/Dialog/FavoriteGenre',
            controller: 'favoriteGenreDialogController'
        });
        modalInstance.close();
    };

Because of that construction, I'm opening and closing the modal each time I press the cancel button. There is anyway so I can close the modal when I press the close button even if the cancel() funcion is in another controller that is in another file? I tried to put the cancel() function on genres.js but I wasn't able to make the html call the genresController without getting lot of erros because of the genresControllers other methods.

ANSWER

find out! the problem was that in the FavoriteGenre.cshtml I was doing

    <div ng-controller="favoriteGenreDialogController" block-ui="genresContentDiv">

I just needed to let it be like:

    <div  block-ui="genresContentDiv">

and it worked using the

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
バカです
  • 175
  • 2
  • 4
  • 18
  • as per my understanding you are trying to execute a function which is in different controller. am i right.. – Raghav Sep 20 '17 at 13:00

1 Answers1

0

Try this.

$scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
Nikita
  • 437
  • 2
  • 12
  • if I do only that I get the "$uibModalInstance is not defined" error. If I inject it, the modal doesn't close and also stops working like it should. – バカです Sep 20 '17 at 17:11