slonick
slonick

Reputation: 181

Dynamic content and AngularJS

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?

Upvotes: 1

Views: 998

Answers (1)

slonick
slonick

Reputation: 181

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.

Upvotes: 2

Related Questions