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.

When using the ngFor loop and two-way data-binding, I want to receive better error message

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

I use Primeng components. When I use ng-templates, I can not create a component, with a link to the instance. Angular compiler sends incomprehensible errors.

<pre>{{cars|json}}</pre>
<p-dataTable [value]="cars">
   
    <p-column header="FieldInstance:">
      <ng-template let-car="rowData" let-index="rowIndex" pTemplate="body">
        [{{index}}]: {{car|json}}
        <car-entity [(value)]="car"></car-entity>
      </ng-template>
    </p-column>

</p-dataTable>
ERROR in : Error: Cannot assign to a reference or variable!
    at _AstToIrVisitor.visitPropertyWrite (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:26426:23)
    at PropertyWrite.visit (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:4722:24)
    at convertActionBinding (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:25850:45)
    at C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:28450:22
    at Array.forEach (<anonymous>)
    at ViewBuilder._createElementHandleEventFn (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:28446:18)
    at nodes.(anonymous function) (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:27865:27)
    at C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:28391:22
    at Array.map (<anonymous>)
    at ViewBuilder._createNodeExpressions (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:28390:56)
    at ViewBuilder.build (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:27627:23)
    at C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:27626:63
    at Array.forEach (<anonymous>)
    at ViewBuilder.build (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:27626:23)
    at ViewCompiler.compileComponent (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:27527:60)
    at AotCompiler._compileComponent (C:\Users\splincode\Desktop\dev\rosnfo-ui\rosnfo-ui\node_modules\@angular\compiler\bundles\compiler.umd.js:31006:62)

https://stackblitz.com/edit/angular-eiskqo?file=app/app.component.html

Expected behavior

If you comment on a piece of code, you can see that car is an object that you can use in the future. But I can not create a component …

https://stackblitz.com/edit/angular-bd7std?file=app/app.component.html

image

<pre>{{cars|json}}</pre>
<p-dataTable [value]="cars">
   
    <p-column header="FieldInstance:">
      <ng-template let-car="rowData" let-index="rowIndex" pTemplate="body">
        [{{index}}]: {{car|json}}
        <!--<car-entity [(value)]="car"></car-entity>-->
      </ng-template>
    </p-column>

</p-dataTable>

Of course, you can bypass the error in this way:

<pre>{{cars|json}}</pre>
<p-dataTable [value]="cars">
   
    <p-column header="FieldInstance:">
      <ng-template let-car="rowData" let-index="rowIndex" pTemplate="body">
        [{{index}}]: {{car|json}}
        <car-entity [(value)]="cars[index]"></car-entity>
        <!-- OR: <car-entity [value]="car" (valueChange)="valueChange()"></car-entity> -->
      </ng-template>
    </p-column>

</p-dataTable>

But in my opinion this is a cant, if we can not normally logically handle the code according to the syntax [(**)]. I would like to know the cause of the error.

https://stackblitz.com/edit/angular-c356jp?file=app/app.component.html

image

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-eiskqo?file=app/app.component.html

Angular version: 5.x.x

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
lacroixdavid1commented, Jan 30, 2018

@splincode This is not related to ng-template, it’s *ngFor related. You can’t reassign a *ngFor variable.

This would cause problems too :

     <div *ngFor="let car of cars">
         <car-entity [(value)]="car"></car-entity> 
      </div>

But this would be fine (assuming a variable named myCar is declared in the controller):

     <car-entity [(value)]="myCar"></car-entity> 
     <div *ngFor="let car of cars">
         <car-entity [value]="car"></car-entity> 
      </div>
0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 - 2 Way Binding with NgModel in NgFor
After digging around I need to use trackBy on ngFor. Updated plnkr and code below. Working Plnkr @Component({ selector: 'my-app', ...
Read more >
Angular ngFor - Learn All Features, Not Only For Arrays
In this post we are going to go over the ngFor core directive, namely we are going to go over the following: what...
Read more >
NgFor • Angular - codecraft.tv
We use the NgFor directive to loop over an array of items and create multiple elements dynamically from a template element. The template...
Read more >
Structural directives - Angular
This guide is about structural directives and provides conceptual information on how such directives work, how Angular interprets their shorthand syntax, ...
Read more >
How to Use Built-In Angular Directive: ngIf, ngFor, ngClass ...
You can use ngFor if you need a certain block to repeat many times. For example, if you had a list of items...
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