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

Commit 0a73b1f

Browse files
feat($compile): allow require to be an object
This provides an elegant alternative to the array form of the `require` property but also helps to support binding of `require`d controllers to directive controllers.
1 parent 7b991f9 commit 0a73b1f

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/ng/compile.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,16 @@
271271
*
272272
* #### `require`
273273
* Require another directive and inject its controller as the fourth argument to the linking function. The
274-
* `require` takes a string name (or array of strings) of the directive(s) to pass in. If an array is used, the
275-
* injected argument will be an array in corresponding order. If no such directive can be
276-
* found, or if the directive does not have a controller, then an error is raised (unless no link function
277-
* is specified, in which case error checking is skipped). The name can be prefixed with:
274+
* `require` property can be a string, an array or an object:
275+
* * a **string** containing the name of the directive to pass to the linking function
276+
* * an **array** containing the names of directives to pass to the linking function. The argument passed to the
277+
* linking function will be an array of controllers in the same order as the names in the `require` property
278+
* * an **object** whose property values are the names of the directibes to pass to the linking function. The argument
279+
* passed to the linking function will also be an object with matching keys, whose values will be the corresponding
280+
* controller.
281+
*
282+
* If no such directive(s) can be found, or if the directive does not have a controller, then an error is raised
283+
* (unless no link function is specified, in which case error checking is skipped). The name can be prefixed with:
278284
*
279285
* * (no prefix) - Locate the required controller on the current element. Throw an error if not found.
280286
* * `?` - Attempt to locate the required controller or pass `null` to the `link` fn if not found.
@@ -2295,6 +2301,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
22952301
for (var i = 0, ii = require.length; i < ii; i++) {
22962302
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
22972303
}
2304+
} else if (isObject(require)) {
2305+
value = {};
2306+
forEach(require, function(controller, property) {
2307+
value[property] = getControllers(directiveName, controller, $element, elementControllers);
2308+
});
22982309
}
22992310

23002311
return value || null;
@@ -2401,6 +2412,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24012412
removeControllerBindingWatches =
24022413
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
24032414
}
2415+
2416+
if (isObject(controllerDirective.require) && !isArray(controllerDirective.require)) {
2417+
var controllers = getControllers(name, controllerDirective.require, $element, elementControllers);
2418+
console.log(controllers);
2419+
}
24042420
}
24052421

24062422
// Trigger the `$onInit` method on all controllers that have one

test/ng/compileSpec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5700,6 +5700,28 @@ describe('$compile', function() {
57005700
});
57015701
});
57025702

5703+
it('should support multiple controllers as an object hash', function() {
5704+
module(function() {
5705+
directive('c1', valueFn({
5706+
controller: function() { this.name = 'c1'; }
5707+
}));
5708+
directive('c2', valueFn({
5709+
controller: function() { this.name = 'c2'; }
5710+
}));
5711+
directive('dep', function(log) {
5712+
return {
5713+
require: { myC1: '^c1', myC2: '^c2' },
5714+
link: function(scope, element, attrs, controllers) {
5715+
log('dep:' + controllers.myC1.name + '-' + controller.myC2.name);
5716+
}
5717+
};
5718+
});
5719+
});
5720+
inject(function(log, $compile, $rootScope) {
5721+
element = $compile('<div c1 c2><div dep></div></div>')($rootScope);
5722+
expect(log).toEqual('dep:c1-c2');
5723+
});
5724+
});
57035725

57045726
it('should instantiate the controller just once when template/templateUrl', function() {
57055727
var syncCtrlSpy = jasmine.createSpy('sync controller'),

0 commit comments

Comments
 (0)