Skip to content

Commit bb4e94f

Browse files
committed
feat($compile): expose publicLinkFn options
Use `options` object hash style parameter for publicLinkFn. Expose `options` parameter as part of the public API. Closes angular#9413
1 parent 660f916 commit bb4e94f

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/ng/compile.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,17 @@
622622
*
623623
*
624624
* @param {string|DOMElement} element Element or HTML string to compile into a template function.
625-
* @param {function(angular.Scope, cloneAttachFn=)} transclude function available to directives.
625+
* @param {function(angular.Scope, cloneAttachFn=)} transclude function available to directives - DEPRECATED.
626+
*
627+
* <div class="alert alert-error">
628+
* **Note:** Passing a `transclude` function to the $compile function is deprecated, as it
629+
* e.g. will not use the right outer scope. Please pass the transclude function as a
630+
* `parentBoundTranscludeFn` to the link function instead.
631+
* </div>
632+
*
626633
* @param {number} maxPriority only apply directives lower than given priority (Only effects the
627634
* root element(s), not their children)
628-
* @returns {function(scope, cloneAttachFn=)} a link function which is used to bind template
635+
* @returns {function(scope, cloneAttachFn=, options=)} a link function which is used to bind template
629636
* (a DOM element/tree) to a scope. Where:
630637
*
631638
* * `scope` - A {@link ng.$rootScope.Scope Scope} to bind to.
@@ -637,6 +644,19 @@
637644
* * `clonedElement` - is a clone of the original `element` passed into the compiler.
638645
* * `scope` - is the current scope with which the linking function is working with.
639646
*
647+
* * `options` - An optional object hash with linking options. If `options` is provided, then the following
648+
* keys may be used to control linking behavior:
649+
*
650+
* * `parentBoundTranscludeFn` - the transclude function made available to
651+
* directives; if given, it will be passed through to the link functions of
652+
* directives found in `element` during compilation.
653+
* * `transcludeControllers` - an object hash with keys that map controller names
654+
* to controller instances; if given, it will make the controllers
655+
* available to directives.
656+
* * `futureParentElement` - defines the parent to which the `cloneAttachFn` will add
657+
* the cloned elements; only needed for transcludes that are allowed to contain non html
658+
* elements (e.g. SVG elements). See also the directive.controller property.
659+
*
640660
* Calling the linking function returns the element of the template. It is either the original
641661
* element passed in, or the clone of the element if the `cloneAttachFn` is provided.
642662
*
@@ -1155,9 +1175,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
11551175
maxPriority, ignoreDirective, previousCompileContext);
11561176
compile.$$addScopeClass($compileNodes);
11571177
var namespace = null;
1158-
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers, parentBoundTranscludeFn, futureParentElement) {
1178+
return function publicLinkFn(scope, cloneConnectFn, options) {
11591179
assertArg(scope, 'scope');
11601180

1181+
options = options || {};
1182+
var parentBoundTranscludeFn = options.parentBoundTranscludeFn,
1183+
transcludeControllers = options.transcludeControllers,
1184+
futureParentElement = options.futureParentElement;
1185+
11611186
// When `parentBoundTranscludeFn` is passed, it is a
11621187
// `controllersBoundTransclude` function (it was previously passed
11631188
// as `transclude` to directive.link) so we must unwrap it to get
@@ -1335,7 +1360,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13351360
transcludedScope.$$transcluded = true;
13361361
}
13371362

1338-
return transcludeFn(transcludedScope, cloneFn, previousBoundTranscludeFn, controllers, futureParentElement);
1363+
return transcludeFn(transcludedScope, cloneFn, {
1364+
parentBoundTranscludeFn: previousBoundTranscludeFn,
1365+
transcludeControllers: controllers,
1366+
futureParentElement: futureParentElement
1367+
});
13391368
};
13401369

13411370
return boundTranscludeFn;

src/ng/directive/ngInclude.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ var ngIncludeFillContentDirective = ['$compile',
284284
$compile(jqLiteBuildFragment(ctrl.template, document).childNodes)(scope,
285285
function namespaceAdaptedClone(clone) {
286286
$element.append(clone);
287-
}, undefined, undefined, $element);
287+
}, {futureParentElement: $element});
288288
return;
289289
}
290290

test/ng/compileSpec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5221,7 +5221,9 @@ describe('$compile', function() {
52215221
tElement.empty();
52225222
return function(scope, element, attrs, ctrls, transcludeFn) {
52235223
element.append(content);
5224-
$compile(content)(scope, undefined, transcludeFn);
5224+
$compile(content)(scope, undefined, {
5225+
parentBoundTranscludeFn: transcludeFn
5226+
});
52255227
};
52265228
}
52275229
};

0 commit comments

Comments
 (0)