Skip to content

Attribute two way binding not working with $any and error is unclear #51165

Closed

Description

@roma2341

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

compiler, core

Is this a regression?

No

Description

Example:
<input [(ngModel)]="$any(name)">
I understand that it's impossible, but error message is completely unclear.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-n2yh4n?file=src%2Fmain.ts

Please provide the exception or error you saw

`Parser Error: Unexpected token '=' at column 12 in [$any(name) =$event] in /~/src/main.ts@11:24`

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

angular 16.1.6
Node v18.12.1

Anything else?

No response

Activity

  1. added
    area: compiler
    P4
    on Jul 25, 2023
  2. modified the milestone: Backlog on Jul 25, 2023
  3. changed the title [-]ngModel two way binding not works with $any and error is unclear[/-] [+]ngModel two way binding not working with `$any` and error is unclear[/+] on Jul 25, 2023
  4. roma2341 commented on Jul 25, 2023

    @roma2341
    Author

    But this is easy example, in my main project i have more unclear errors (this is actually why i created this issue, i understand how to resolve my issue, but it take a while and really is a bug):

    Error: src/app/components/events/event/event-card/event-card.component.html:106:56 - error NG5002: Parser Error: 
    Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:107:53 - error NG5002: Parser Error: Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:109:55 - error NG5002: Parser Error: Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:109:55 - error NG5002: Parser Error: Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:109:55 - error NG5002: Parser Error: Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:116:40 - error NG5002: Parser Error: Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:116:40 - error NG5002: Parser Error: Unexpected token '='
    
    Error: src/app/components/events/event/event-card/event-card.component.html:117:45 - error NG5002: Parser Error: Unexpected token '='
    

    And if first two errors make sense, further errors appears on completely good lines e.g:
    <comments-card [targetID]="value?.id" [entityType]="getEntityType()"></comments-card>
    It starts to hate every '=' after the place where i had [(attribute)]="$any(...)".

    But i can't reproduce that very bad case and can't share my production code.
    When i separated [(attribute)] into [attribute] and (attributeChange) in single place the hundreds of Unexpected token '=' errors disappeared.
    Sorry i checked it was because of my custom attribute, not ngModel

  5. changed the title [-]ngModel two way binding not working with `$any` and error is unclear[/-] [+]Attribute two way binding not working with `$any` and error is unclear[/+] on Jul 25, 2023
  6. roma2341 commented on Jul 25, 2023

    @roma2341
    Author

    You can remove this issue, i can't reproduce what i really want to reproduce =(

  7. AbobakarSadeeq commented on Jul 26, 2023

    @AbobakarSadeeq

    Here is the solution:
    https://stackblitz.com/edit/angular-hxyrrs?file=src%2Fmain.html,src%2Fmain.ts

    I have solve your issue in the above stackblitz link but the way you want to do the problem is not a well practice and good approach to do it.

    Question:

    1. why you want to use the $any with the two way binding directive?

    If you really want to assign the any to the type-members in angular then just when initilizing the type-member assign the "any" type there but still not recommended to assign that type because it makes occuption memory alot and also reading the code become more complex.

    Also do not use the $any type with the ngModel (two way binding) because it is not a valid approach to do that. just simply make the normal property and assign its reference.
    name: string = 'Angular';
    <input [(ngModel)]="name" />

  8. roma2341 commented on Jul 26, 2023

    @roma2341
    Author

    @AbobakarSadeeq I understand that i can't use $any with two way binding and i understand how to solve a problem, but error message should be more clear, because in one case it was completely wrong - "error NG5002: Parser Error: Unexpected token '='", and it's hard to find culprit because compiler reports first place where wrong two-way binding met and further places where it met '=' symbol (correct bindings). I thought that i reproduced that issue, but on my stackblitz error was different:
    Parser Error: Unexpected token '=' at column 12 in [$any(name) =$event] in /~/src/main.ts@11:24 (it works as exptected and doesn't report further '=' symbols as unexpected tokens.

  9. alxhub commented on Mar 29, 2024

    @alxhub
    Member

    The error is at least better now:

    NG5002: Unsupported expression in a two-way binding

    $any should work, though. Tagging this as a compiler bug.

  10. added
    P3
    and removed
    P4
    on Mar 29, 2024
  11. self-assigned this
    on Jan 4, 2025
  12. added a commit that references this issue on Jan 4, 2025
    4018917
  13. added a commit that references this issue on Jan 6, 2025
    ceadd28
  14. angular-automatic-lock-bot commented on Feb 6, 2025

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

  15. locked and limited conversation to collaborators on Feb 6, 2025
  16. added a commit that references this issue on Feb 7, 2025
    5b683c6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: compilerbugcompiler: template type-checkinghotlist: error messagesstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions