Component and Directive selector property doesn't actually restrict by element tag or attribute
See original GitHub issueTitle is pretty self explanatory, so here’s an example:
@Directive({ selector: '[some-directive]' })
export class MyDirective {
constructor(@Inject('$element') $element) {
console.log($element);
}
}
This directive will log the creation of a controller on both <some-directive></some-directive>
and <div some-directive></div>
event though the query selector specified should only match attribute values. This happens vice versa for directives made with @Component
.
I’m assuming this is not intended behavior and I’m not expected to inlcude legacy: { restrict: 'A' }
in all my directive definitions.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
What is the difference between component and directive?
Component: A view with associated behaviour. This type of directive actually adds DOM elements; Attribute directives: Can be attached to DOM ...
Read more >Angular directives | decorator | Attributes | Structural - Medium
Normally a directive is considered like an element's attribute. The directive selector is ... Actually, this syntax is for components.
Read more >Angular Directive Selectors Can Do More Than You Think!
In this article, we have learned that you don't have to use the attribute selector when working with Angular directives.
Read more >Angular Components with Non-Standard Selectors
In reality, the selector of both components and directives can be any CSS ... HTML element, and use an attribute to activate the...
Read more >Angular Directive Tutorial With Example | Custom Directives
Attribute Directive is basically used to modify or alter the appearance and behavior of an element. The selector is the property that identifies ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Haha yes, true. I actually only discovered it by accident when trying to port over ng-forward’s DOM event directives to my project when the
'input'
directive started behaving strange (matching<input>
elements). And then that’s where I ran into #129 😛Anyway, I gave up on that and just switched everything back to
ng-${event}
.Can we have it so that the selector provided is actually mapped to a restriction? A case that I need is a directive on all
<video>
elements to do something when theloadedmetadata
event fires. The selector I should use in this case would be'video'
, not'[video]'
, but it won’t work if use@Directive
. Angular 2 supports this use.Using
legacy: { restrict: 'E' }
, works around the issue, but the change in 2.2.0 was actually breaking for me.