0

I am trying to add an angular JS module inside an already existing code. I'm actually trying to add a new piece of code inside it. The workflow goes like this. When a button is clicked, the JS takes a template and adds it to the dom. In this added template, i would like to insert the new angular JS module. I am trying to atleast show hello world inside the module but haven't been successful.

Let's say when a button is clicked, i'm adding the below tag to the dom

<div patient-details ng-controller='patientDetailsCtrl' id='patientDetails'></div>

This is added through jquery or javascript. How do i make it work with the below directive

angular.module('patientDetailsModule',[])
.directive('patientDetails',function($compile){
    return{
        //restrict: 'E',
        template: "<div>Hello World!!!{{name}}</div>"
    };
});
1
  • I did not understand :(. Could you explain? Commented Aug 27, 2014 at 20:11

1 Answer 1

1

You need to use $compile. $compile() will make sure all the Angular code in your html snippet will be compiled!

Here is an example.

http://plnkr.co/edit/7Wr3oAtvZ8cn31uFFEOs?p=preview

  • Send Message from nc-click to directive
  • Directive receives message, compiles html and appends new element

                var template = "<div patient-details ng-controller='patientDetailsCtrl' id='patientDetails'>Element added</div>";
                var newElement = angular.element(template);
                $compile(newElement)(scope);
                element.append(newElement);
                // can also use element.replaceWith(newElement);
    
Sign up to request clarification or add additional context in comments.

Comments

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.