Skip to content

Add extended diagnostic to warn when an attribute is not interpreted as a binding #46137

Closed

Description

@antch

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Angular does not properly bind an attribute when using attr.foo syntax if there is no interpolation present in the value.

For example, this does not work:

<div attr.foo="bar"></div> <!-- erroneously keeps the attribute as "attr.foo" -->

This DOES work:

<div attr.foo="{{myProp}}"></div>

This also works:

<div [attr.foo]="'bar'"></div>

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-ybks1w?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.module.ts

Please provide the exception or error you saw

The attribute is not set properly on the element using `attr.foo` syntax when no interpolation is present.

Please provide the environment you discovered this bug in (run ng version)

The reproduction link is using Angular 13, but I also encountered this in my project which produces the following output for `ng version`:


Angular CLI: 12.2.16
Node: 14.19.3
Package Manager: npm 6.14.17
OS: darwin x64

Angular: 12.2.16
... animations, cli, common, compiler, compiler-cli, core
... elements, forms, language-service, localize
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.16
@angular-devkit/build-angular   12.2.16
@angular-devkit/core            12.2.16
@angular-devkit/schematics      12.2.16
@angular/cdk                    12.2.13
@schematics/angular             12.2.16
ng-packagr                      12.2.7
rxjs                            6.6.3
typescript                      4.3.5
webpack                         5.50.0


### Anything else?

I encountered this in my project previously on Angular 11 as well.

Activity

  1. added this to the needsTriage milestone on May 25, 2022
  2. changed the title [-]String attribute bindings don't work properly when no interpolation is used[/-] [+]Add extended diagnostic to warn when an attribute is not interpreted as a binding[/+] on May 26, 2022
  3. added
    featureLabel used to distinguish feature request from other issues
    area: compilerIssues related to `ngc`, Angular's template compiler
    and removed
    area: coreIssues related to the framework runtime
    on May 26, 2022
  4. modified the milestones: needsTriage, Backlog on May 26, 2022
  5. atscott commented on May 26, 2022

    @atscott
    Contributor

    This is working as expected. There's nothing in the attribute assignment that indicates it's a binding. A binding in the value of an attribute makes the attribute assignment a binding. [attr.foo] is also an attribute binding.

    I've updated the title to make this a feature request for the extended diagnostics feature to warn when attr.x is not interpreted as a binding. This could also include style. and class.

  6. pkozlowski-opensource commented on May 26, 2022

    @pkozlowski-opensource
    Member

    @atscott yeh, I didn't close this one as I can totally see how it could be confusing. For a user it is not at all clear why attr.x="{{exp}}" sets x attribute and attr.x="static" sets attr.x. In a sense the left-hand side of the assignment (target) changes based on the rhs and I would consider this surprising. So it "works as designed" but probably not "as expected".

    As an alternative to warning we could just set the x attribute for the attr.x="static" case.

  7. 11 remaining items

  8. added
    feature: in backlogFeature request for which voting has completed and is now in the backlog
    and removed on Jun 28, 2022
  9. angular-automatic-lock-bot commented on Jul 31, 2022

    @angular-automatic-lock-bot

    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.

  10. locked and limited conversation to collaborators on Jul 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions