md-button-raised buttons (click) not working inside of *ngFor
See original GitHub issueBug, feature request, or proposal:
Bug
<button md-raised-button>
's that are generated within a template via *ngFor do not register click event.
What is the expected behavior?
That when a md-raised-button with the (click) event is generated inside of ngFor it will be clickable or fire click event.
What is the current behavior?
Button will not respond to clicks at all. Removing md-raised-button from the element will allow the button to work as expected.
What are the steps to reproduce?
<div *ngFor="let value of someIterable; let i='index'">
<button md-raised-button (click)="someFunction(value)">{{value}}</button>
</div>
Which versions of Angular, Material, OS, browsers are affected?
“@angular/core”: “2.0.0”, “@angular/material”: “^2.0.0-alpha.9-3”, Chrome Version 53.0.2785.116 (64-bit) OSX Yosemeti 10.10.5 (14F1912)
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Mat-Button click inside a *ngFor with let index ... - Stack Overflow
After that the mat-buttons are working inside the *ngFor again. import { Component } from '@angular/core'; import { FormControl, FormGroup, ...
Read more >Angular ngFor - Learn All Features, Not Only For Arrays
Let's learn a ton of hidden ngFor features that are not often mentioned, but mot of all we are going to learn how...
Read more >Change Specific Button Text On Click Inside Ngfor - ADocLib
Change Specific Button Text On Click Inside Ngfor. Template variables help you use data from one part of a template in another part...
Read more >Angular NgFor, <ng-template> - the complete guide
In this post you're going to learn how to use Angular's NgFor directive to loop over data to render data or components.
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issue here is that you are asking the array() function to return a new array each time change detection is run, which is building a new set of buttons each time. This happens so often while you are clicking that angular is unable to successfully evaluate the click handler.
Add a trackBy function that uses the object’s id so that the ngFor doesn’t recreate an element and you’ll see that everything works fine: http://plnkr.co/edit/0vYMY2Hv2OUWqZBe0nPf?p=preview
A better solution would be to make that array a property of your component rather than something dynamically built each time the function is called. You’ll get much better performance this way: http://plnkr.co/edit/VzA7igNhrt9ylG3lS5g7?p=preview
Can’t reproduce(http://plnkr.co/edit/qgCm0mRFCd09FWUM3Fin?p=preview).
Provide a plunk where it doesn’t work.
@zahlprish @jelbourn