joemfb / ml-common-ng

An angular module of common components for the MarkLogic REST API
https://joemfb.github.io/ml-common-ng/
2 stars 2 forks source link

Add compile directive #4

Closed grtjn closed 9 years ago

grtjn commented 9 years ago

Very useful when pulling HTML (static or generated with a REST transform, potentially containing angular instructions) that you want to inject into your view:

// Copied from https://docs.angularjs.org/api/ng/service/$compile
angular.module('sample.create')
  .directive('compile', function($compile) {
    'use strict';

    // directive factory creates a link function
    return function(scope, element, attrs) {
      scope.$watch(
        function(scope) {
           // watch the 'compile' expression for changes
          return scope.$eval(attrs.compile);
        },
        function(value) {
          // when the 'compile' expression changes
          // assign it into the current DOM
          element.html(value);

          // compile the new DOM and link it to the current
          // scope.
          // NOTE: we only compile .childNodes so that
          // we don't get into infinite loop compiling ourselves
          $compile(element.contents())(scope);
        }
      );
    };
  });

Since it is being removed from our slush template, I'd be grateful to give it a new home here so I can still easily make use of it..

joemfb commented 9 years ago

Sounds good to me, could you whip it into a PR?

grtjn commented 9 years ago

I'll try to create a PR asap..