In this example, how can you dynamically add a directive in AngularJS? You have a lot of pointless jQuery in there, but the $compile service is actually super simple in this case:
1 2 3 4 5 6 7 8 9 10 11 12 13 | .directive( 'test', function ( $compile ) { return { restrict: 'E', scope: { text: '@' }, template: '<p ng-click="add()">{{text}}</p>', controller: function ( $scope, $element ) { $scope.add = function () { var el = $compile( "<test text='n'></test>" )( $scope ); $element.parent().append( el ); }; } }; }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.