uidevangular
uidevangular

Reputation: 11

How to get hold of a value from modal popup angular js

I have a input box in modal popup , i want that value in my main controller which invoked the modal.

Upvotes: 1

Views: 626

Answers (1)

Appeiron
Appeiron

Reputation: 1061

Easy way:

//Modal controller
$rootScope.$broadcast('modal:nameOfModal:onSave', { data: data })

//Any other controller
$scope.$on('modal:nameOfModal:onSave', function(event, data) { ... } )

Right way:

  • Modal creator
  • Factory with methods representing each modal type you need
  • Each method method of Factory (like Factory.confirmationWindow()) returning a promise that should be handled separately in controllers that rise appropriate modal
  • Call something like in your controller

    Controller.propotype.openModal = function() {
        ModalsFactory.confirmationModal().then(function(result) {
            //using result inside of your controller
            //if clicked ok
        }).catch(function() {
            //behavior to be called when modal canceled 
        });
    }
    

Pros:

  1. Totally separates logic
  2. Allow to define different behavior of same modals in different controllers

Cons:

  1. ModalsFactory will be big file

Upvotes: 1

Related Questions