Skip to content

Instantly share code, notes, and snippets.

@raghava
Last active August 29, 2015 14:02
Show Gist options
  • Save raghava/195ef7b512c13923c8d6 to your computer and use it in GitHub Desktop.
Save raghava/195ef7b512c13923c8d6 to your computer and use it in GitHub Desktop.
ngIncludeView.js it'a alternative to ng-include, which load the view & re uses the same scope
// ng-include-view
// - directive in module ng
//
// This works similar to ng-include and it won't create new scope,
// instead it uses the same level
//
// Example usage:
// <ng-include-view src=""></ng-include-view>
'use strict';
angular
.module('App', [])
.directive('ngIncludeView', function($compile, $templateCache) {
return {
restrict: "EA",
link: function($scope, elem, attrs){
var templateUrl = attrs.src;
$scope.$watch(templateUrl, function(){
elem.html($templateCache.get(arguments[0]));
$compile(elem.contents())($scope);
}, true);
}
};
});
@Matt-Jensen
Copy link

Great directive, however arguements[0] is undefined for me. Ended up using "attrs.src" instead.

@Thorsson
Copy link

I have updated the defualt ngInclude into ngInsert which behaves same as the original one except it does inherit the scope. You can also set optional argument preserve-scope to make it behave as regular ngInclude
https://gist.github.com/Thorsson/738c46285828ed95fd79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment