Last active
August 29, 2015 14:02
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}; | |
}); |
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
Great directive, however arguements[0] is undefined for me. Ended up using "attrs.src" instead.