From c68da4e8a5fd7e2bb602f80a11f5851c613c0098 Mon Sep 17 00:00:00 2001 From: jankuca Date: Thu, 8 Aug 2013 16:04:11 -0700 Subject: [PATCH] fix(compile): fix directives with controller, templateUrl and compile properties --- src/ng/compile.js | 16 +++++------ test/ng/compileSpec.js | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index ad979e337c43..1618bcede9c8 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -782,13 +782,6 @@ function $CompileProvider($provide) { directiveName = directive.name; - if (directiveValue = directive.controller) { - controllerDirectives = controllerDirectives || {}; - assertNoDuplicate("'" + directiveName + "' controller", - controllerDirectives[directiveName], directive, $compileNode); - controllerDirectives[directiveName] = directive; - } - if (directiveValue = directive.transclude) { assertNoDuplicate('transclusion', transcludeDirective, directive, $compileNode); transcludeDirective = directive; @@ -877,6 +870,13 @@ function $CompileProvider($provide) { } } + if (!directive.templateUrl && directive.controller) { + controllerDirectives = controllerDirectives || {}; + assertNoDuplicate("'" + directiveName + "' controller", + controllerDirectives[directiveName], directive, $compileNode); + controllerDirectives[directiveName] = directive; + } + if (directive.terminal) { nodeLinkFn.terminal = true; terminalPriority = Math.max(terminalPriority, directive.priority); @@ -1157,7 +1157,7 @@ function $CompileProvider($provide) { origAsyncDirective = directives.shift(), // The fact that we have to copy and patch the directive seems wrong! derivedSyncDirective = extend({}, origAsyncDirective, { - controller: null, templateUrl: null, transclude: null, scope: null, replace: null + templateUrl: null, transclude: null, scope: null, replace: null }), templateUrl = (isFunction(origAsyncDirective.templateUrl)) ? origAsyncDirective.templateUrl($compileNode, tAttrs) diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 5dfad4be7f29..6262e0318962 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -2502,6 +2502,68 @@ describe('$compile', function() { expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;') }); }); + + it('should allow controller usage in directives with templateUrl', function () { + module(function () { + var Ctrl = function (log) { + log('instance'); + }; + + directive('myDirective', function () { + return { + scope: true, + templateUrl: 'hello.html', + controller: Ctrl, + compile: function () { + return { + pre: function (scope, template, attr, ctrl) {}, + post: function () {} + }; + } + }; + }); + }); + + inject(function ($templateCache, $compile, $rootScope, log) { + $templateCache.put('hello.html', '

Hello

'); + + element = $compile('
')($rootScope); + $rootScope.$apply(); + + expect(log).toEqual('instance'); + expect(element.text()).toBe('Hello'); + }); + }); + + it('should allow controller usage in directives with a template', function () { + module(function () { + var Ctrl = function (log) { + log('instance'); + }; + + directive('myDirective', function () { + return { + scope: true, + template: '

Hello

', + controller: Ctrl, + compile: function () { + return { + pre: function (scope, template, attr, ctrl) {}, + post: function () {} + }; + } + }; + }); + }); + + inject(function ($templateCache, $compile, $rootScope, log) { + element = $compile('
')($rootScope); + $rootScope.$apply(); + + expect(log).toEqual('instance'); + expect(element.text()).toBe('Hello'); + }); + }); });