question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component and Directive selector property doesn't actually restrict by element tag or attribute

See original GitHub issue

Title 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kherockcommented, Aug 20, 2016

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}.

0reactions
kherockcommented, Oct 9, 2016

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 the loadedmetadata 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found