1

I'm playing with angularjs and dynamic content of a page. The problem is the page can't have a template because I need to display a representation of multiple different objects fetched from the server.

Currently I'm $watching the contents of a body element for changes and then compile the whole contents of the body. It works. But I have a feeling that there is a better way to do what I need.

Again, basically I fetch a JSON representation of an object, build an HTML representation and append it to the body. Then watch fires up and compiles the contents of the body.

Any ideas?

1
  • Can you show us what you're trying? A fiddle or plnkr.co perhaps? Commented Jan 18, 2013 at 14:36

1 Answer 1

2

Ok, I got it. Extensive googling always help.

There is a better way of doing this. First determine the targetElement that you will append to and then create the html you want to append.

Then

var element = angular.element(targetElement);
var scope = element.scope();
var injector = element.injector();
var compile = injector.get('$compile');

compile(html)(scope).appendTo(targetElement);

May be this is a basic stuff but I am just learning the AngularJS framework.

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

1 Comment

You shouldn't need to use the injector to get the $compile service. You should be able to inject the $compile service using dependency injection into your function. If you use a directive, you won't have to call angular.element(). If you write a directive with a compile function, you won't have to call $compile(). You might not even need to compile anything (manually or via a directive). If you want to post some code, we could probably give you a better solution.

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.