8000 feat($compile): allow required controllers to be bound to the directi… · angular/angular.js@b2c0b05 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b2c0b05

Browse files
feat($compile): allow required controllers to be bound to the directive controller
If directives are required through an object hash, rather than a string or array, the required directives' controllers are bound to the current directive's controller in much the same way as the properties are bound to using `bindToController`. The binding is done after the controller has been constructed and all the bindings are guaranteed to be complete by the time the controller's `$onInit` method is called. This change makes it much simpler to access require controllers without the need for manually wiring them up in link functions. In par 10000 ticular this enables support for `require` in directives defined using `mod.component()`
1 parent f72ecbd commit b2c0b05

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/ng/compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,8 +2414,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24142414
}
24152415

24162416
if (isObject(controllerDirective.require) && !isArray(controllerDirective.require)) {
2417-
var controllers = getControllers(name, controllerDirective.require, $element, elementControllers);
2418-
console.log(controllers);
2417+
var controllerObj = getControllers(name, controllerDirective.require, $element, elementControllers);
2418+
extend(controller.instance, controllerObj);
24192419
}
24202420
}
24212421

test/ng/compileSpec.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5338,6 +5338,48 @@ describe('$compile', function() {
53385338
});
53395339
});
53405340

5341+
it('should bind the required controllers to the directive controller, if provided as an object', function() {
5342+
var parentController, siblingController;
5343+
5344+
function ParentController() { this.name = 'Parent'; }
5345+
function SiblingController() { this.name = 'Sibling'; }
5346+
function MeController() { this.name = 'Me'; }
5347+
MeController.prototype.$onInit = function() {
5348+
parentController = this.container;
5349+
siblingController = this.friend;
5350+
};
5351+
spyOn(MeController.prototype, '$onInit').andCallThrough();
5352+
5353+
angular.module('my', [])
5354+
.directive('me', function() {
5355+
return {
5356+
restrict: 'E',
5357+
scope: {},
5358+
require: { container: '^parent', friend: 'sibling' },
5359+
controller: MeController
5360+
};
5361+
})
5362+
.directive('parent', function() {
5363+
return {
5364+
restrict: 'E',
5365+
scope: {},
5366+
controller: ParentController
5367+
};
5368+
})
5369+
.directive('sibling', function() {
5370+
return {
5371+
controller: SiblingController
5372+
};
5373+
});
5374+
5375+
module('my');
5376+
inject(function($compile, $rootScope, meDirective) {
5377+
element = $compile('<parent><me sibling></me></parent>')($rootScope);
5378+
expect(MeController.prototype.$onInit).toHaveBeenCalled();
5379+
expect(parentController).toEqual(jasmine.any(ParentController));
5380+
expect(siblingController).toEqual(jasmine.any(SiblingController));
5381+
});
5382+
});
53415383

53425384
it(&# 9E1A 39;should require controller of an isolate directive from a non-isolate directive on the ' +
53435385
'same element', function() {
@@ -5712,7 +5754,7 @@ describe('$compile', function() {
57125754
return {
57135755
require: { myC1: '^c1', myC2: '^c2' },
57145756
link: function(scope, element, attrs, controllers) {
5715-
log('dep:' + controllers.myC1.name + '-' + controller.myC2.name);
5757+
log('dep:' + controllers.myC1.name + '-' + controllers.myC2.name);
57165758
}
57175759
};
57185760
});
@@ -9488,5 +9530,5 @@ describe('$compile', function() {
94889530
}));
94899531
});
94909532
});
9491-
});
9533+
});
94929534
});

0 commit comments

Comments
 (0)
0