From 90f87072e83234ae366cfeb3c281503c31dad738 Mon Sep 17 00:00:00 2001
From: Tobias Bosch
Date: Thu, 14 Nov 2013 13:50:36 -0800
Subject: fix($compile): accessing controllers of transcluded directives from
children
Additional API (backwards compatible)
- Injects `$transclude` (see directive controllers) as 5th argument to directive link functions.
- `$transclude` takes an optional scope as first parameter that overrides the
bound scope.
Deprecations:
- `transclude` parameter of directive compile functions (use the new parameter for link functions instead).
Refactorings:
- Don't use comment node to temporarily store controllers
- `ngIf`, `ngRepeat`, ... now all use `$transclude`
Closes #4935.
---
test/ng/directive/ngIfSpec.js | 28 ++++++++++++++++++++++++++++
test/ng/directive/ngIncludeSpec.js | 30 ++++++++++++++++++++++++++++++
test/ng/directive/ngRepeatSpec.js | 27 +++++++++++++++++++++++++++
3 files changed, 85 insertions(+)
(limited to 'test/ng/directive')
diff --git a/test/ng/directive/ngIfSpec.js b/test/ng/directive/ngIfSpec.js
index 3173f476..427bfd59 100755
--- a/test/ng/directive/ngIfSpec.js
+++ b/test/ng/directive/ngIfSpec.js
@@ -148,6 +148,34 @@ describe('ngIf', function () {
});
+describe('ngIf and transcludes', function() {
+ it('should allow access to directive controller from children when used in a replace template', function() {
+ var controller;
+ module(function($compileProvider) {
+ var directive = $compileProvider.directive;
+ directive('template', valueFn({
+ template: '
',
+ replace: true,
+ controller: function() {
+ this.flag = true;
+ }
+ }));
+ directive('test', valueFn({
+ require: '^template',
+ link: function(scope, el, attr, ctrl) {
+ controller = ctrl;
+ }
+ }));
+ });
+ inject(function($compile, $rootScope) {
+ var element = $compile('')($rootScope);
+ $rootScope.$apply();
+ expect(controller.flag).toBe(true);
+ dealoc(element);
+ });
+ });
+});
+
describe('ngIf animations', function () {
var body, element, $rootElement;
diff --git a/test/ng/directive/ngIncludeSpec.js b/test/ng/directive/ngIncludeSpec.js
index beb29da7..aba71e44 100644
--- a/test/ng/directive/ngIncludeSpec.js
+++ b/test/ng/directive/ngIncludeSpec.js
@@ -439,6 +439,36 @@ describe('ngInclude', function() {
});
});
+describe('ngInclude and transcludes', function() {
+ it('should allow access to directive controller from children when used in a replace template', function() {
+ var controller;
+ module(function($compileProvider) {
+ var directive = $compileProvider.directive;
+ directive('template', valueFn({
+ template: '',
+ replace: true,
+ controller: function() {
+ this.flag = true;
+ }
+ }));
+ directive('test', valueFn({
+ require: '^template',
+ link: function(scope, el, attr, ctrl) {
+ controller = ctrl;
+ }
+ }));
+ });
+ inject(function($compile, $rootScope, $httpBackend) {
+ $httpBackend.expectGET('include.html').respond('');
+ var element = $compile('')($rootScope);
+ $rootScope.$apply();
+ $httpBackend.flush();
+ expect(controller.flag).toBe(true);
+ dealoc(element);
+ });
+ });
+});
+
describe('ngInclude animations', function() {
var body, element, $rootElement;
diff --git a/test/ng/directive/ngRepeatSpec.js b/test/ng/directive/ngRepeatSpec.js
index 9dde36e7..6584f31a 100644
--- a/test/ng/directive/ngRepeatSpec.js
+++ b/test/ng/directive/ngRepeatSpec.js
@@ -1058,6 +1058,33 @@ describe('ngRepeat', function() {
});
});
+describe('ngRepeat and transcludes', function() {
+ it('should allow access to directive controller from children when used in a replace template', function() {
+ var controller;
+ module(function($compileProvider) {
+ var directive = $compileProvider.directive;
+ directive('template', valueFn({
+ template: '
',
+ replace: true,
+ controller: function() {
+ this.flag = true;
+ }
+ }));
+ directive('test', valueFn({
+ require: '^template',
+ link: function(scope, el, attr, ctrl) {
+ controller = ctrl;
+ }
+ }));
+ });
+ inject(function($compile, $rootScope) {
+ var element = $compile('')($rootScope);
+ $rootScope.$apply();
+ expect(controller.flag).toBe(true);
+ dealoc(element);
+ });
+ });
+});
describe('ngRepeat animations', function() {
var body, element, $rootElement;
--
cgit v1.2.3