AngularJS ngInclude directive and scope inheritance

ngInclude directive and scope

There are many times when you want to include a html snippet code from another file but preserve the scope of it. It is usually when you have different form fields for the various objects and you want to have a global controller that oversees the updating of different forms. So if you want to take the quickest route and use ngInclude directive you would be surprised that it is not properly linking to your controller and you cannot access the form instance.

This is due to ngInclude internals and how they work. ngInclude creates for each use as a new child scope so overwriting anything inside the new included HTML file content will be written into child scope and not in the one you’ve anticipated to be. So there are few workaround around this as creating a new object inside the scope for example

$ = {}

inside the controlling controller and then in the imported html file set values inside the

<input type="text" ng-model=""/>

This works if you don’t have a problem with static value being inserted into all html files, but if you want maximum flexibility then this is not the perfect solution. So after inspecting the source code inside ngInclude.js, I have seen a room for improvement and created a similar directive to ngInclude called ngInsert, which instead of making new child scope it inherits the current scope and continue using it inside. You can pick up the whole source code at this gist. You can use it in the same manner as existing ngInclude.

<ng-insert src="groupForm.html"></ng-insert>

I am also appending here the full source code for further reading.

  'use strict';
    .module('app', [])
    .directive('ngInsert', ngInsert)
    .directive('ngInsert', ngInsertFillContentDirective);
  ngInsert = ['$templateRequest', '$anchorScroll', '$animate', '$sce'];
  function ngInsert($templateRequest, $anchorScroll, $animate, $sce) {
    return {
      restrict: 'ECA',
      priority: 400,
      terminal: true,
      transclude: 'element',
      controller: angular.noop,
      compile: function(element, attr) {
        var srcExp = attr.ngInsert || attr.src,
            onloadExp = attr.onload || '',
            preserveScope = attr.preserveScope || true,
            autoScrollExp = attr.autoscroll;
        return function(scope, $element, $attr, ctrl, $transclude) {
          var changeCounter = 0,
          var cleanupLastInsertContent = function() {
            if (previousElement) {
              previousElement = null;
            if (currentScope) {
              currentScope = null;
            if (currentElement) {
              $animate.leave(currentElement).then(function() {
                previousElement = null;
              previousElement = currentElement;
              currentElement = null;
          scope.$watch($sce.parseAsResourceUrl(srcExp), function ngIncludeWatchAction(src) {
            var afterAnimation = function() {
              if (angular.isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
            var thisChangeId = ++changeCounter;
            if (src) {
              $templateRequest(src, true).then(function(response) {
                if (thisChangeId !== changeCounter) return;
                var newScope = scope.$parent;
                if (!preserveScope)
                  newScope = scope.$new();
                ctrl.template = response;
                var clone = $transclude(newScope, function(clone) {
                  $animate.enter(clone, null, $element).then(afterAnimation);
                currentScope = newScope;
                currentElement = clone;
                currentScope.$emit('$insertContentLoaded', src);
              }, function() {
                if (thisChangeId === changeCounter) {
                  scope.$emit('$insertContentError', src);
              scope.$emit('$insertContentRequested', src);
            } else {
              ctrl.template = null;
  ngInsertFillContentDirective = ['$compile'];
  function ngInsertFillContentDirective($compile) {
    return {
      restrict: 'ECA',
      priority: -400,
      require: 'ngInsert',
      link: function(scope, $element, $attr, ctrl) {
        if (/SVG/.test($element[0].toString())) {
          // WebKit: --- SVG elements do not
          // support innerHTML, so detect this here and try to generate the contents
          // specially.
          $compile(jqLiteBuildFragment(ctrl.template, document).childNodes)(scope,
              function namespaceAdaptedClone(clone) {
          }, {futureParentElement: $element});

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.