1

Let me first post the explanation of what I am trying to accomplish with broken-pseudocode:

// controllers.js
angular.module('myApp.controllers', []).
  controller('MyCtrl1', [function() {
     var template = '<div my-directive></div>';
     var div = $("#view1").append(template);
     var code = http.get('/dynamic-code-for-directive');
     angular.module('myApp.directives').directive(eval(code));

  }])

// Index.html
<html lang="en" ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>My AngularJS App</title>
  <link rel="stylesheet" href="css/app.css"/>
</head>
<body>
  <ul class="menu">
    <li><a id="view1" href="#/view1">view1</a></li>       
  </ul>
</body>
</html>

// code.js returned by "GET /dynamic-code-for-directive"
function(scope, element){ $("#view1").append("Hello World");

Explanation

Basically I want the server to provide the definition for the directive, and then generate one on the fly. The problem, it seems, is with the append call above --- it does not recognize the template as the directive, so the directive code never gets called.

Has anyone ever tried to accomplish anything similar to this?

1
  • Please, select best answer if you got your answer. :) Commented Mar 14, 2014 at 15:35

2 Answers 2

1

try doing it in the following order

 var code = http.get('/dynamic-code-for-directive');
 angular.module('myApp.directives').directive(eval(code)); //ie adding the directive definition

then make a template but one step that you are missing is COMPILING it

     $scope.template = $compile(<div my-directive></div>')($scope);

then append it!

angular.element("#view1").append($scope.template);

please revert back with the results

Sign up to request clarification or add additional context in comments.

4 Comments

Where do I get the $compile from? I.e. I can't inject it into controller for some reason.
your controller also has $scope missing in it write [function($scope,$compile)]
I did, but $compile is undefined at that point. Does it require some kind of module dependency, do you know?
look out for different ways of defining your module i am sure there is something wrong in the way you are defining your angular module.Controllers have access to $compile or it can also be an issue with your angular js version please upgrade to the latest one.
0

There is some things you are doing wrong, you are doing DOM manipulation inside a controller, which is wrong and not an appropriate use of angularjs.

To use $compile you need to be inside a directive. It would look something like:

app.directive('directiveCreator', function($compile, $http) {
  return function(scope, element, attrs) {
    var template = '<div my-directive></div>';
    element.html(template);
    var code = $http.get('/dynamic-code-for-directive');
    app.directive('myDirective', function() {
       var link = eval(code);
      return {
        link : link
      }
    }
    $compile(element.contents())(scope);
  }
  );
};

There might be mistakes but it would be something like that.

4 Comments

So I need a directive to create a directive? :-) But I don't know ahead of time how many directives there will be! Hm...I'll have to give it more thought.
But I don't know ahead of time how many directives I will need. So I will need directiveCreator1, directiveCreator2, etc.. But if I need to create THESE directives dynamically, I am back to square one: how do I create directives on the fly?
You only need one directiveCreator. What does tell you how many directives you need?
The server will. For example, imagine application with dynamic number of tabs at the top. Clicking on the tab fetches a dynamic template and a "directive" that will know how to handle the html in that template. So, one template may contain, say a table (with sorting and other custom behavior), while another --- some sort of form. The point is that each tab's content would be defined via a template dynamically loaded from the server, and it's behavior --- by a dynamically loaded code (which I was thinking would be possible to write as a directive). Hope this is not too confusing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.