I'm struggling with my AngularJS directive that involves calling $compile within a click() function callback. Despite my efforts, the contents are not being compiled as expected.
var testApp = angular.module('testApp', []);
testApp.controller('TestController', function($scope) {
$scope.user_name = 'George';
});
testApp.directive('testDirective', function($compile) {
return {
template: 'Click Here',
link: function(scope, element, attrs) {
$(element).click(function() {
$('#externalDiv2').html('{{user_name}}, you clicked the button.');
$compile($('#externalDiv2')[0])(scope);
});
}
};
});
Even in the given example, #externalDiv2 still shows '{{user_name}}' instead of actually interpolating it with the username from the scope object.
Can anyone point out what I might be doing wrong?