On the docs I saw an example of compiling "something" added later.
var $div = $('<div ng-controller="MyCtrl">{{content.label}}</div>');
$(document.body).append($div);
angular.element(document).injector().invoke(function($compile) {
var scope = angular.element($div).scope();
$compile($div)(scope);
});
I've added this code on a jquery ready function, but I have two problems:
First is an error:
Argument 'MyCtrl' is not a function, got undefined
.
The second is that I don't know how to make that content.label
works! I've added it to the scope
but it doesn't work. How should I call my controller to see working the data binding of content.label
?
MY FINAL MODIFIED CODE IS:
var app = angular.module('app',[]);
$(function(){
app.controller('MyCtrl',function($scope){
$scope.content = 123;
});
var $div = $('<div ng-controller="MyCtrl">{{content}}</div>');
$(document.body).append($div);
angular.element(document).injector().invoke(function($compile) {
var scope = angular.element($div).scope();
$compile($div)(scope);
});
});