Skip to content Skip to sidebar Skip to footer

Angularjs Open Modal From Controller

I am new to angularjs, and writing an application where a template(inventory.html) containing grid is opened with , it has a validate button at bottom of grid, which calls validat

Solution 1:

I would strongly suggest you write a separate controller for you modal. That will greatly help you to maintain your code later.

If I understand correctly (from your tags) you use an this one: https://angular-ui.github.io/bootstrap/ From your main controller, open the modal:

var modalInstance = $modal.open({
      animation: $scope.animationsEnabled,
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return$scope.items;
        }
      }
    });

note the resolve function, you can pass extra parameters like this to modal controller.

In modal controller:

angular.module('your.awesomeApp').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {

note the third parameter, it will be values, that you have passed.

Solution 2:

You can use ui.bootstrap's components which include a modal. You won't have to use a separate controller.

Solution 3:

Look this example:

/* Main controller */
App.controller("JustificativaModalController",  function($scope, $modal) {
    var modalInstance = $modal.open({
        controller : "ControllerModal",
        templateUrl : "TemplateModal.html",
        resolve: {
            $callback: function () {
                returnfunction(param){
                    /* This function print value(param) dispached in modal controller */
                    console.log(param);
                };
            },
            $send: function(){
                returnfunction(){
                    /* This function send param for the modal */return {param1:"Hello Word Main"};
                };
            }
        }
    });
});

/* Modal controller */
App.controller("JustificativaModalController", function($scope, $modalInstance, $callback, $send) {
    $scope.init = function(){
        /* This function print value(param) dispached in main controller */
        console.log($send);

        /* This send data to main controller */$callback({param1:"Hello Word Modal"});
    };
});

Solution 4:

This solved my problem plnkr. Thanks @Remigijus, @Emir Marques

$scope.open = function (user) {
          $scope.user = user;
          $modal.open({
              templateUrl: 'myModalContent.html',
              backdrop: true,
              windowClass: 'modal',
              controller: function ($scope, $modalInstance, $log, user) {
                  $scope.user = user;
                  $scope.submit = function () {
                      $log.log('Submiting user info.');
                      $log.log(JSON.stringify(user));
                      $modalInstance.dismiss('cancel');
                  }
                  $scope.cancel = function () {
                      $modalInstance.dismiss('cancel');
                  };
              },
              resolve: {
                  user: function () {
                      return$scope.user;
                  }
              }
          });
      };

Post a Comment for "Angularjs Open Modal From Controller"