Help with AngularJS Modal

Hi..
I have a controller that opens a modal window.
How can I change the templateUrl value when the modal window is opened?

  $ionicModal.fromTemplateUrl('modal-details.html', {
       scope: $scope,
        animation: 'slide-in-up',
        hardwareBackButtonClose: true
    }).then(function (modal) {
        $scope.modal = modal;
    });


// Open window here..

    $scope.openModal = function(selected){

       // How can I change the templateUrl based on the selected value?
     

        $scope.data.selected = selected;
LVL 1
JElsterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle HamiltonData ScientistCommented:
you might try something like this, though I have not actually tested it.

in the controller:
var self = this;
var templateurl = 'default-template'

$ionicModal.fromTemplateUrl(templateurl+'.html', { // so we don't mix dot notation with bracket notation
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    self[templateurl] = modal; // creates a modal object on the this context with the same name as the template
  });

  $scope.openModal = function(templateurl) {
    self[templateurl].show();
  };

Open in new window



in the view:

  <a ng-click="openModal('my-custom-template')">Open my custom template</a>

Open in new window

JElsterAuthor Commented:
I get
 
JavaScript runtime error: Unable to get property 'show' of undefined or null reference

at

 self[templateurl].show();

any ideas ?

thx
Kyle HamiltonData ScientistCommented:
yeah.. i thought that might happen. it's too late. the modal is alteady initialized with the default template. if you want to test my theory you could set the default templare variable to the same one that you pass in the click function. if it works, then my theory is correct, otherwise something else is screwey and i'd have to set up an app to test it.

unfortunately it doesn't appear that ionic modal service has the capabilty of specifying a template at run time
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Kyle HamiltonData ScientistCommented:
you could also try having separate instances of the ionic modal that use different templates. in the .then function set $scope.anotherModal = modal

then, do

$scope.anotherModal.show()

does that make sense? (typing on phone)
JElsterAuthor Commented:
Yes.. I'll try that
thx
Kyle HamiltonData ScientistCommented:
Scrap the above suggestions. This should work. Since you are executing the function at run time, instead of upon initialization of the controller.

$scope.showModal = showModal;

function showModal(template){
	$ionicModal.fromTemplateUrl(template, {
        scope: $scope,
        animation: 'slide-in-up',
        hardwareBackButtonClose: true
    }).then(function (modal) {
        modal.show()
    });
}

Open in new window


 <a ng-click="showModal('path/to/template.html')">Open my custom template</a>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
thx!
that worked!
Kyle HamiltonData ScientistCommented:
cool :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.