So I basically want to be able to trigger an event and then have a directive compile and insert its self to a position in the DOM.
Currently I have something like this
//controller
angular.module('app').controller('MainCtrl', function ($scope, $compile) {
$scope.$on('insertItem',function(ev,attrs){
var el = $compile( "<chart></chart>" )( $scope );
$scope.insertHere = el;
});
});
// directive
angular.module('app')
.directive('chart', function () {
return {
template: '<div>My chart</div>',
restrict: 'E',
link: function postLink(scope, element, attrs) {
element.text('this is a chart');
}
};
});
I am able to see the object "el" with all that I need but I'm not able to insert it into the DOM...
any clues?
question from:
https://stackoverflow.com/questions/16656735/insert-directive-programmatically-angularjs 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…