Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

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;
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

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

Avatar of JElster

ASKER

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

at

 self[templateurl].show();

any ideas ?

thx
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
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)
Avatar of JElster

ASKER

Yes.. I'll try that
thx
ASKER CERTIFIED SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

thx!
that worked!
cool :)