components icon indicating copy to clipboard operation
components copied to clipboard

md-button-raised buttons (click) not working inside of *ngFor

Open zahlprish opened this issue 10 years ago • 9 comments

Bug, 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)

zahlprish avatar Oct 05 '16 03:10 zahlprish

Can't reproduce(http://plnkr.co/edit/qgCm0mRFCd09FWUM3Fin?p=preview).

Provide a plunk where it doesn't work.

@zahlprish @jelbourn

fxck avatar Oct 07 '16 14:10 fxck

Tried this also, could not reproduce.

jelbourn avatar Nov 01 '16 00:11 jelbourn

Button not working with function in ngFor (*ngFor="let item of items()") http://plnkr.co/edit/s2ALzvNN2QUowz4PawiS

Knoxvillekm avatar Jan 24 '17 13:01 Knoxvillekm

That looks like an old plunker. Works fine with a more recent one: http://plnkr.co/edit/Orp9e9I5R8KVdyvqILJv?p=preview

jelbourn avatar Jan 24 '17 17:01 jelbourn

It does not work with a function that returns an array of objects: http://plnkr.co/edit/UEIUigPIRoNpZB47VNTO?p=preview button without md-button works fine

Knoxvillekm avatar Jan 24 '17 17:01 Knoxvillekm

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

andrewseguin avatar Jan 24 '17 19:01 andrewseguin

Thank you.

Knoxvillekm avatar Jan 24 '17 20:01 Knoxvillekm

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.

@andrewseguin Overall I agree that this is a fine alternative solution but in regards to:

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.

Lets say you had full control over change detection by turning it all off and updating explicitly (standard for Immutable based apps), would this still be the case?

zahlprish avatar Jan 25 '17 00:01 zahlprish

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.