Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a scenario where i need to apply different directives (attribute) to a DIV inside a Angular bootstrap Modal at runtime (button click).

I would know the name of the directive to apply. But i am not able to figure out how to change the template at runtime to add necessary directive name as an attribute to the DIV.

consider this plunker

Basically i want the div to have child directive as an attribute using synstax like this <div {{child}}></div>

So when it works, it should generate <div child-directive></div>

How can this be done? is this even possible? What is the best way to change the template before opening the Modal so that it wires up correctly when loaded.

share|improve this question
up vote 1 down vote accepted
// Code goes here

var app = angular.module('main-module', ['ui.bootstrap']);

app.directive('parentDirective', function($uibModal, $compile) {
    return {
      restrict: 'E',
      template: "<h2>I am Parent</h2><button ng-click='click()'>Click Me</button>",
      scope: {
        child:'@'
      },
      link: function($scope, elem, attrs) {
        console.log('?',$scope.child, $scope);

        var template = "<div><h3>This is modal</h3>" 
              + "Ideally you should see the child directive below"
              + "<hr />"
              + "<div "+ $scope.child + "></div></div>"; 

        $scope.click = function() {
          $uibModal.open({
            template: template,
            scope: $scope,
            size: 'lg',
          });
        }
      }
    };
  })
  .directive('childDirective', function() {
    return {
      restrict: 'A',
      template: "<div><h4>I am Child</h4><a ng-click='click()'>Click Me!!</a></div>",
      replace: true,
      scope: {},
      link: function($scope, elem, attrs) {
        $scope.click = function() {
          alert("I am in child scope");
        }
      }
    };
  }).directive('anotherChildDirective', function() {
    return {
      restrict: 'A',
      template: "<div><h4>I am another Child</h4><a ng-click='click()'>Click Me!!</a></div>",
      replace: true,
      scope: {},
      link: function($scope, elem, attrs) {
        $scope.click = function() {
          alert("I am in child scope");
        }
      }
    };
  });;
share|improve this answer
    
Can you share a plunker where it works? I tried what you said above and it doesnt work.I still dont see child directive in the modal – sagar Jan 22 at 12:39
    
I see your plunker has hardcoded directive. That misses the entire point. I want to specifiy the directive name at runtime. You have changed the modalContent.html like this. (<div child-directive></div>) but i dont want to do that. I need to evaluate the scope variable and place that value as an attribute to the div. – sagar Jan 22 at 12:42
    
Ok i miss understood i think it will be problem with register and execute child directive with your way – Mike86 Jan 22 at 14:04
    
ok i found solution for this problem :) plunker is ready here:plnkr.co/edit/Du5tmdjCmJDsmJxeGbhK?p=preview if this are satisifed you please accept my answer – Mike86 Jan 22 at 15:39
    
After trying hard to somehow make it work the way i wanted to as per my question above, i did eventually did the same as Mike86, I had to use template instead of templateUrl, and generate template at runtime. Hence marking this as answer. – sagar yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.