Skip to content

Instantly share code, notes, and snippets.

@PitBeast
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PitBeast/5ca6fd627fc3a1ed1010 to your computer and use it in GitHub Desktop.
Save PitBeast/5ca6fd627fc3a1ed1010 to your computer and use it in GitHub Desktop.
Directives for show part of some template in other template
'use strict';
angular
.module('portal', [''])
.directive('portalIn', [function() {
return {
scope: {
key: '@',
portalContainer: '='
},
link: {
pre: function($scope, $element) {
if ($scope.key) {
if (angular.isObject($scope.portalContainer[$scope.key])) {
angular.extend(
$scope.portalContainer[$scope.key],
{
scope: $scope.$parent,
template: $element.html(),
trigger: true
}
);
} else {
$scope.portalContainer[$scope.key] = {
scope: $scope.$parent,
template: $element.html(),
trigger: true
}
}
}
$element.remove();
}
}
}
}])
.directive('portalOut', ['$compile', function($compile) {
return {
scope: {
key: '@',
portalContainer: '='
},
link: function($scope, $element) {
$scope.$watch(
function() {
return $scope.portalContainer[$scope.key]
? $scope.portalContainer[$scope.key].trigger
: undefined;
},
function(showContent) {
if (showContent && angular.isObject($scope.portalContainer[$scope.key])) {
var content = $compile(
$scope.portalContainer[$scope.key].template
)($scope.portalContainer[$scope.key].scope);
$element.empty().append(content);
delete $scope.portalContainer[$scope.key].trigger;
}
}
);
}
}
}])
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment