0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
A F
  • 7,424
  • 8
  • 40
  • 52
  • Just making sure I'm understanding this correctly. You have element `
    Hello
    ` and you want it to become `
    Hello
    ` ? Or do you also want to pass the specific function to bind?
    – tasseKATT Jan 12 '15 at 10:13
  • @tasseKATT your first example is essentially right. I want to write `
    ` in my html, then I want it to become `
    ` after angular has done its thing. The main reason I don't want to use a template is to preserve any random attributes that might be on the same element. ps I don't really care if the html shows up like that, but basically I want my `add` directive to 'inherit' the `ng-click` directive
    – A F Jan 12 '15 at 17:26
  • I can show you how in a while, but why don't you just use `ng-click`? Just trying to understand the use case. – tasseKATT Jan 12 '15 at 18:05

0 Answers0