ngfor

Does ngFor directive re-render whole array on every mutation?

感情迁移 提交于 2019-11-28 10:51:52
Let's say we have an array of items: items = [ { title: 'item 1'}, { title: 'item 2'}, /* ... */ ]; And there is a template that renders this array: <ul> <li *ngFor="let item of items">{{item.title}}</li> </ul> Wll angular2 rerender the whole array if I add/remove items via push / splice or will it only add/remove the markup for the corresponding items? If it does updates only, then is there any difference in mutation stategies -- should I prefer push/splice over array replacing? In other words, are these two approaches equivalent in term of rendering performance: /* 1: mutation */ this.items

how to bind img src in angular 2 in ngFor?

不问归期 提交于 2019-11-28 07:10:18
In my project I am getting data: image src , student name and student id . I bind student name and student id . How to bind image src in angular 2 ? Angular 2, 4 and Angular 5 compatible! You have provided so few details, so I'll try to answer your question without them. You can use Interpolation: <img src={{imagePath}} /> Or you can use a template expression: <img [src]="imagePath" /> In a ngFor loop it might look like this: <div *ngFor="let student of students"> <img src={{student.ImagePath}} /> </div> Angular 2.x to 8 Compatible! You can directly give the source property of the current

Angular 2 Click + ngClass, how to apply ONLY to SELF but not ALL elements inside ngFor

喜欢而已 提交于 2019-11-28 04:56:00
问题 In Angular 1.x this following code works as I want to to click and flip a card inside an ng-repeat <div class="card" ng-repeat="let card of cards"> <div class="flipcard" ng-class="{'flipped':isflipped}" ng-click="isflipped = !isflipped"> </div> </div> However in Angular 2 when clicked, it flipped every "card" inside the ngFor loop... how do I bind the ngClass condition to the element itself only? <div class="card" *ngFor="let card of cards"> <div class="flipcard" [ngClass]="{'flipped':

angular2 toggle icons inside ngFor [duplicate]

泄露秘密 提交于 2019-11-28 03:55:46
问题 This question already has answers here : Hide/show individual items inside ngFor (5 answers) Closed 2 years ago . Can some one please let me know how to toggle icons while doing ngFor? Problem Statement: I'm using *ngFor to loop through an array and display category names. On click of day I need to open an accordian and show category details (I can do this). Once accordian opens I need to replace fa-plus icon with fa-minus and vice-versa and I need to do this only for clicked day. How can I

How to show 1 element in ngFor in angular2?

◇◆丶佛笑我妖孽 提交于 2019-11-28 00:32:01
I have a simple ngFor that loops through an array. However, I've added a condition that while the index is < 5 keep add the tag. and After that, I want to add an extra tag just once that will be used a dropdown to view the rest of the tags. But it doesn't work. <li *ngFor="let tag of module.Tags; let i = index"> <a *ngIf="i<5" href="#" class="span-tag tag">{{ tag }}</a> <div *ngIf="module.Tags.length > 5 && i == 6">DropDown Button</div> </li> The feature here is that I don't want to show unlimited number of tags to the user, I want to limit it to only 5 tags, and have a button after 5 tags

What is the difference between [ngFor] and [ngForOf] in angular2?

夙愿已清 提交于 2019-11-27 21:47:10
As per my understanding, Both are doing the same functions. But, ngFor would be works like as collections ? ngForOf would be works like as generics ? Is my understanding is correct? or Could you please share more difference's(details) about ngFor and ngForOf ? ngFor and ngForOf are not two distinct things - they are actually the selectors of the NgForOf directive. If you examine the source , you'll see that the NgForOf directive has as its selector: [ngFor][ngForOf] , meaning that both attributes need to be present on an element for the directive to 'activate' so to speak. When you use *ngFor

How to ngFor on multiple types of components using ngComponentOutlet?

微笑、不失礼 提交于 2019-11-27 16:32:28
问题 I've got a whatsapp-like chat case of many types of messages that are needed to be displayed differently. Each has its' own component such as TextMessageComponent , FileMessageComponent , etc.. I'd like to be able to ngFor once on my array of messages without having to ngIf over the types. I've heard ngComponentOutlet might be the solution but found it hard to implement.. Any suggestions, a mini demo or anything you find useful would be highly appreciated! 回答1: Having a property on the object

function gets called several times

对着背影说爱祢 提交于 2019-11-27 15:13:59
I want to display a dataList. Some values are calculate from a function. It seems angular2 calls the calculate function many times. <tr *ngFor="let data of dataList"> <td>{{ data.no }}</td> <td>{{ data.name }}</td> <td>{{ calculateFunction(data.price) }}</td> </tr> Console will output "calculate..." many times, more than dataList.length. calculateFunction() { console.log('calculate...'); return ...; } Should I worry about that for performance or just let angular2 do this? The caculateFunction(data.price) function will be called every time Angular runs change detection for the component (more

How to create variable in ngFor loop?

痞子三分冷 提交于 2019-11-27 14:17:27
I am trying to find out how to create a variable in an ngFor loop . I have a loop like this: <td *ngFor="#prod of products"> <a href="{{getBuild(branch,prod)?.url}}"> {{getBuild(branch,prod)?.status}} </a> </td> You can see the the getBuild call has to be repeated multiple times. (Many more times in my actual example). I would like a way to create this variable in the template once inside the loop and simply reuse it. Is there a way to do this? I think local variables (defined with the # character) don't apply for your use case. In fact, when you define a local variable on an HTML element it

How to use trackBy with ngFor

感情迁移 提交于 2019-11-27 13:33:33
I can't really understand what I should return from trackBy . Based on examples I've seen on the web, I should return the value of some property on the object. Is it correct? Why do I get index as a parameter? For example, in the following case: constructor() { window.setInterval(() => this.users = [ {name: 'user1', score: Math.random()}, {name: 'user2', score: Math.random()}], 1000); } userByName(index, user) { return user.name; } ... <div *ngFor="let user of users; trackBy:userByName">{{user.name}} -> {{user.score}}</div> The objects shown in template are still updated despite the name being