The following is how you can dynamically add an ng-click
directive. Lets say I want to do the same thing, but I don't want to use a template. I want to add ng-click to the current element. How Can I do that?
.directive( 'test', function ( $compile ) { return {
restrict: 'E',
scope: { text: '@' },
template: '<p ng-click="add()">{{text}}</p>',
replace:true,
controller: function ( $scope, $element ) {
$scope.add = function () {
var el = $compile( "<test text='n'></test>" )( $scope );
$element.parent().append( el );
};
} }; });
This is essentially the same question as this post, but I want to accomplish without using a template