[Representation] Use transclusion for mct-include

...to add/remove conditionally depending on the existence of
certain templates.

Note that this currently breaks mct-include due to an incompatibility
between element transclusion and directive templates; see
https://github.com/angular/angular.js/issues/3368.
This commit is contained in:
Victor Woeltjen 2015-10-28 10:29:41 -07:00
parent 757cb0f015
commit 942f617bd8

View File

@ -69,19 +69,39 @@ define(
templateMap[key] = templateMap[key] || path;
});
function controller($scope) {
function link($scope, element, attrs, ctrl, transclude) {
var originalElement = element,
activeElement = element;
$scope.$watch('key', function (key) {
// Pass the template URL to ng-include via scope.
$scope.inclusion = templateMap[$scope.key];
if (templateMap[key]) {
// Pass the template URL to ng-include via scope.
$scope.inclusion = templateMap[$scope.key];
// ...and add the template to the DOM.
transclude(function (clone) {
activeElement.replaceWith(clone);
activeElement = clone;
});
} else if (activeElement !== originalElement) {
// If the key is unknown, remove it from DOM entirely.
activeElement.replaceWith(originalElement);
activeElement = originalElement;
}
});
}
return {
transclude: 'element',
priority: 601,
terminal: true,
// Only show at the element level
restrict: "E",
// Use the included controller to populate scope
controller: controller,
link: link,
// Use ng-include as a template; "inclusion" will be the real
// template path