You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Grr, detection of dangling attributes would really help to catch cases of using directives in a template without listing a given directive in @View. Maybe as a first step we could throw for all attributes that:
were not matched by any directive
have - in their names
are not on elements with names containing -
are not data- (and / or x-?) prefixed
While not perfect it should be able to catch some common errors.
@mhevery nothing particular stops us, apart that it needs work. Here are the items to take care of:
- need to have a way of knowing which attributes were "consumed" by a directive (selector + "static" property bindings)
- need to have a way of knowing what are legitimate attributes on both regular HTML elements (we can hand-code this in schema from HTML specs) and web components (this will require hand-written schema by devs)
So, it is definitively doable but require some work - we can put it in one of the releases, depending on other priorities.
need to have a way of knowing what are legitimate attributes on both regular HTML elements (we can hand-code this in schema from HTML specs) and web components (this will require hand-written schema by devs)
The problem is that we would need a way to tell Angular what attributes we don't mind appearing. People do use "unknown" attributes in custom elements but also with 3rd party libraries that are working outside Angular.
The additional cost of providing an API to specify this compared to the benefit that compiler errors provide is not high. Especially considering how few comments and votes this issue has received.
Regarding your point @th0r - we cannot remove title attribute since it might be used for CSS styling etc.
I don't suggest to remove any attribute, I suggest to fix the most common case when component's input has the same name as some already existing html attribute and not set this attribute in this case. It can be easily done as I believe Angular compiler has all necessary information for this. If one really needs to set this html attribute, he can always do it via [attr.].
My concern is that more new arrtibutes may appear in html in the future which potentially may overlap with input names on your already existing components and it may break their behavior.
I don't suggest to remove any attribute, I suggest to fix the most common case when component's input has the same name as some already existing html attribute and not set this attribute in this case. It can be easily done as I believe Angular compiler has all necessary information for this. If one really needs to set this html attribute, he can always do it via [attr.].
Then the rendered HTML should not contain the title attribute? Is this not equivalent to removing that attribute?
In any case I think that this use case is probably not the same as detecting dangling attributes as outlined in this issue, so perhaps it should be discussed separately?
Frankly speaking, I don't understand why Angular sets attributes for co-named inputs on component's host element at all. IMO it's very confusing and strange as most of them simply become [object Object].
In any case I think that this use case is probably not the same as detecting dangling attributes as outlined in this issue, so perhaps it should be discussed separately?
I posted it here because of this comment - it says that issue with title is blocked by this one which has been closed.
I think that comment @th0r is not correct. That issue is not dependent upon this one.
I don't think it is wise for the Angular compiler to error here. There are many cases where you actually do want to have the attribute appear; moreover, if you do not want an attribute to appear then you simply use a square bracket binding to a string instead.
Rather I think the right approach to support developers it to vote for the issue on angular-eslint as mentioned by @splincode.
OK, thanks, but that's a directive and I'm talking about components with custom tag selectors and I can't think of any usecase when you want their inputs to also be set as host attributes. Anyway, for such cases we could have an extra decorator for inputs e.g. @Attr which would tell Angular to also set corresponding attribute to the host element.
Thanks for the additional viewpoint @th0r - This checking can be achieved through linting, without creating a breaking change to how the Angular compiler works, so we do not intend to implement this feature.
angular-automatic-lock-bot commented on Oct 9, 2021
Activity
pkozlowski-opensource commentedon Aug 11, 2015
Grr, detection of dangling attributes would really help to catch cases of using directives in a template without listing a given directive in
@View. Maybe as a first step we could throw for all attributes that:-in their names-data-(and / orx-?) prefixedWhile not perfect it should be able to catch some common errors.
mhevery commentedon Aug 21, 2015
@pkozlowski-opensource what is stoping up from implementing this? I thought we had something already work.ng
pkozlowski-opensource commentedon Aug 21, 2015
@mhevery nothing particular stops us, apart that it needs work. Here are the items to take care of:
So, it is definitively doable but require some work - we can put it in one of the releases, depending on other priorities.
mhevery commentedon Jul 1, 2016
implemented
pkozlowski-opensource commentedon Jul 1, 2016
I believe that this is still an issue. If I have an attribute
fooobut meant to kick-off directivefooit will fail silently.pkozlowski-opensource commentedon Sep 30, 2016
Re-opening as this is not implemented, see #12016 as an example.
16 remaining items
th0r commentedon Sep 3, 2021
But why Angular compiler can't detect that component has a
titleinput and don't set HTML attribute in this case?petebacondarwin commentedon Sep 3, 2021
This is part of the sticking point:
The problem is that we would need a way to tell Angular what attributes we don't mind appearing. People do use "unknown" attributes in custom elements but also with 3rd party libraries that are working outside Angular.
The additional cost of providing an API to specify this compared to the benefit that compiler errors provide is not high. Especially considering how few comments and votes this issue has received.
Regarding your point @th0r - we cannot remove
titleattribute since it might be used for CSS styling etc.th0r commentedon Sep 4, 2021
I don't suggest to remove any attribute, I suggest to fix the most common case when component's input has the same name as some already existing html attribute and not set this attribute in this case. It can be easily done as I believe Angular compiler has all necessary information for this. If one really needs to set this html attribute, he can always do it via
[attr.].th0r commentedon Sep 4, 2021
My concern is that more new arrtibutes may appear in html in the future which potentially may overlap with input names on your already existing components and it may break their behavior.
petebacondarwin commentedon Sep 4, 2021
So you are saying that if there is
where the component has
titleas an input:Then the rendered HTML should not contain the
titleattribute? Is this not equivalent to removing that attribute?In any case I think that this use case is probably not the same as detecting dangling attributes as outlined in this issue, so perhaps it should be discussed separately?
splincode commentedon Sep 4, 2021
@petebacondarwin angular-eslint/angular-eslint#320
I think prefer for correct behaviour restrict input when name conflicted with native attribute.
What do you think about check it when running AOT (if we don't have eslint)?
th0r commentedon Sep 4, 2021
Frankly speaking, I don't understand why Angular sets attributes for co-named inputs on component's host element at all. IMO it's very confusing and strange as most of them simply become
[object Object].I posted it here because of this comment - it says that issue with
titleis blocked by this one which has been closed.petebacondarwin commentedon Sep 4, 2021
I think that comment @th0r is not correct. That issue is not dependent upon this one.
I don't think it is wise for the Angular compiler to error here. There are many cases where you actually do want to have the attribute appear; moreover, if you do not want an attribute to appear then you simply use a square bracket binding to a string instead.
Rather I think the right approach to support developers it to vote for the issue on angular-eslint as mentioned by @splincode.
th0r commentedon Sep 4, 2021
@petebacondarwin could you provide a few examples when you need an attribute for corresponding input to be set on host component?
petebacondarwin commentedon Sep 4, 2021
I'm. not quite sure what you are asking for @th0r - but the Angular Material library has possible examples: the
MatInputdirective has aplaceholderinput, which can be set without creating a square bracket binding, https://material.angular.io/components/form-field/overview#floating-label.th0r commentedon Sep 5, 2021
OK, thanks, but that's a directive and I'm talking about components with custom tag selectors and I can't think of any usecase when you want their inputs to also be set as host attributes. Anyway, for such cases we could have an extra decorator for inputs e.g.
@Attrwhich would tell Angular to also set corresponding attribute to the host element.petebacondarwin commentedon Sep 8, 2021
Thanks for the additional viewpoint @th0r - This checking can be achieved through linting, without creating a breaking change to how the Angular compiler works, so we do not intend to implement this feature.
angular-automatic-lock-bot commentedon Oct 9, 2021
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.