-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat(directive): Add ability to inject required controllers into the controller function #5893
Comments
This issue should be reconsidered. Now that we have m.directive('childComponent', function() {
return {
require: ['childComponent', '^parentComponent'],
controller: function($scope) {
this.init = function(parentComponent) {
this.parentComponent = parentComponent;
// ...
};
},
link: function(scope, el, attrs, ctrls) {
var childComponent = ctrls[0],
parentComponent = ctrls[1];
childComponent.init(parentComponent);
},
// ...
};
}); Why can't this look like this? m.directive('childComponent', function() {
return {
require: '^parentComponent',
controller: function($scope, parentComponent) {
this.parentComponent = parentComponent;
// ...
},
// ...
};
}); |
In general I agree that this is inconvenient, but I think that the main issue with passing the required controller in the controller constructor is that then you will not be able to have symbiotic directive ( |
It's possible not to break the directives of such kind. Because in JS, it's possible to create objects before calling their constructors, and that's how actually |
Still constructor of controller |
That's true, however such symbiotic directives look like an edge case and not the best design choice, don't they? And if you decide to create alike symbiotic services that use each other, it will look confusing enough as well. The DI system won't allow you to do this without a workaround. So can this really be a stopper for this feature? |
I think a "resolve", which works similar to how routing handles controllers, makes more sense. For example, "require" uses $element.controller('directiveName') to look up controllers of other directives. You can simply inject $element into your controller and do it yourself: pseudo non-working off-the-cuff code below:
but I'd rather have a resolve to remove the $element dependency (for testing, etc)
|
Hmmm... I sort of like the idea of Those are concerns, not solutions. I think a good solution to this would be to simply add the controller to Still haven't totally thrown out the value of |
i don't think it would be wise to add yet another way to make synchronous compilation asynchronous... At the very least, it should't be done without combing through the compiler, identifying every single use case that it needs to support, and re-writing it into something sane and maintainable. Adding more sync vs async craziness at this point is just not gonna fly :( |
Yeah, it's wishful thinking more than anything. I think the similarities
|
Actually, it simplifies the DDO. Compare the two snippets I wrote above. |
@kentcdodds see #2095 |
I think that injecting required controller as local is "have to" in new version. |
👍 |
Any news on this? |
+1 |
2 similar comments
+1 |
+1 |
To simplify the implementation, we can introduce a restriction that only the controllers from the element's parents ( |
+1 |
👍 |
2 similar comments
+1 |
+1 |
+1, and also for it to be added to the new |
@hannahhoward had an interesting take on doing this in #12342 |
…oller construction This enables option three of angular#13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See angular#5893
…oller construction This enables option three of angular#13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See angular#5893
…oller construction This enables option three of angular#13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See angular#5893
…oller construction This enables option three of angular#13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See angular#5893
…oller construction This enables option three of angular#13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See angular#5893
…oller construction This enables option three of angular#13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See angular#5893
…oller construction This enables option three of #13510 (comment) by allowing the creator of directive controllers using ES6 classes to have a hook that is called when the bindings are definitely available. Moreover this will help solve the problem of accessing `require`d controllers from controller instances without resorting to wiring up in a `link` function. See #5893 Closes #13763
quick overview
You have the directives A, B and C
B requires A,
C requires B
The methods in C need to call methods in the controller of B.
The methods in the controller of B need call methods in the controller of A.
The controller of B cannot know the controller of A because it can only be passed into the link-function of B.
more details
http://stackoverflow.com/questions/21231294/angularjs-inject-required-directive-controller-into-the-controller-instead-of-t/21231428
Actually there is a workaround that is too complicated for such an easy problem.
Even the ngModelController does use another workaround with a feature that seems to be undocumented: the binding of directive controllers to the directive-elements data property:
angular.js/src/ng/directive/input.js
Line 969 in 5adea0b
The text was updated successfully, but these errors were encountered: