1

I have a form that can be editable. I am trying to set the Summernote plugin when the user clicks a button.

For example, this is the form header, which should get summernote initialized when edit is true, destroyed when edit is false.

<div class="card-header" toggleSummerNote>
  <h2>{{questionnaire.description.title}}</h2>
</div>

How do I make the summernote directive conditional? I tried with this directive, but it does not work. Any suggestions?

app.directive('toggleSummerNote', function ($compile) {
    return {
        restrict: 'A',
        replace: false,
        terminal: true,
        priority: 1000,
        link: function (scope, element, attrs) {
            scope.$watch('edit', function () {
                console.log(scope.edit);
                if (scope.edit) {
                    element.attr("summernote");
                    element.removeAttr("toggle-summer-note"); 
                    $compile(element)(scope);
                }
                else {
                  if (element.hasOwnProperty("summernote")) {
                      element.attr("toggle-summer-note");
                      element.removeAttr("summernote");
                      $compile(element)(scope);
                  }
                }
            });
        }
    };
});

Solution:

Based on Paul's answer below, this is what worked for me:

app.directive('toggleSummerNote', function ($compile) {
    return {
        scope: {
            editing: '='
        },
        compile: function (tElem, tAttrs) {
            return function (scope) {
                scope.$watch('editing', function (newValue, oldValue) {
                    if (newValue !== oldValue) {
                        if (newValue) {
                            tElem.attr('summernote', '');
                            tElem.removeAttr('toggle-summer-note');
                            $compile(tElem)(scope);
                            scope.summernote = true;
                        } else {
                            if (scope.summernote) {
                                tElem.attr('toggle-summer-note', '');
                                tElem.removeAttr('summernote');
                                $compile(tElem)(scope);
                                scope.summernote = false;
                            }
                        }
                    }
                });

            }
        }
    }
});
ganeshk
  • 5,583
  • 3
  • 27
  • 31
  • Have you tried surrounding the summernote element in a div with ng-if="edit" ? – CodeBob Feb 11 '16 at 16:54
  • I have considered that; that seems like a very dirty way of implementing this. Also, I will have atleast 50+ elements to surround with `ng-if` – ganeshk Feb 11 '16 at 16:55
  • http://stackoverflow.com/questions/15696416/what-is-the-best-way-to-conditionally-apply-attributes-in-angular maybe it can help – AshBringer Feb 11 '16 at 16:56
  • @BipBip, thanks, but that is for adding attributes. I want to add a directive which gets through $compile. – ganeshk Feb 11 '16 at 16:58

1 Answers1

2

You could setup a $watch through your directive's compile step, then toggle the summernote attribute there and call $compile on the directive's element to link your summernote directive:

in your directive definition

...
compile: function(tElem, tAttrs) {
  return function(scope) {

    // this avoids looping/other side-effects
    tElem.removeAttr('some-directive', '');

    // editting would be the variable that toggles edit mode
    scope.$watch('editting', function(newValue, oldValue) {
      if (newValue !== oldValue) {
        console.log('editting changed', newValue, oldValue);
        if (newValue) {
          tElem.attr('summernote', '');  
        } else {
          tElem.removeAttr('summernote');
        }

        $compile(tElem)(scope);
      }
    });

  }
}

Here's an example in plnkr, I stubbed out a second directive so you can tell when it's linking/getting removed:

http://plnkr.co/edit/r3mt6h8EocYKfDDr2vkR?p=preview

paul trone
  • 702
  • 5
  • 10
  • Thanks man! That's a brilliant idea. I am working out some kinks, will update my question and accept soon. – ganeshk Feb 11 '16 at 19:13