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.

[Bug] *ngFor in span/formattedString

See original GitHub issue

From @mikraus94 on November 15, 2016 13:29

Hi,

I’m trying to make *ngFor for span but without success.

It stacks texts under selfs

<Label *ngFor="let span of spans> <FormattedString> <Span [text]="span.text" [fontAttributes]="span.att"></Span> </FormattedString> </Label>

It doesn’t work for

<Label> <FormattedString *ngFor="let span of spans> <Span [text]="span.text" [fontAttributes]="span.att"></Span> </FormattedString> </Label>

or

<Label> <FormattedString> <Span *ngFor="let span of spans [text]="span.text" [fontAttributes]="span.att"></Span> </FormattedString> </Label>

It shows only first object from spans array.

Can you help me or this is bug?

Thank you

Copied from original issue: NativeScript/NativeScript#3099

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vakrilovcommented, May 31, 2017

Some updates:

  • Doing <Label><FormattedString *ngFor="let span of spans"> ... will never work as label can have only a single FormattedString.
  • Having multiple spans inside FormattedString is allowed, so <Span *ngFor="let span of spans" ... sounds reasonable. However, when having a structural directive (*ngFor) angular actually inserts a marker element to keep track of where to add and remove the items. This marker not a valid element to be inserted inside FormattedString, so probably this is the reason the code is not working.

You can create a simple pipe to overcome this issue:

@Pipe({ name: 'join' })
export class JoinPipe implements PipeTransform {
    transform(items: any[], field: string): any {
        if (field) {
            items = items.map(i => i[field]);
        }
        return items.join("");
    }
}

//.. and in the template:
<Label [text]="spans | join:'text'"></Label>

In a case you don’t actually need the styling of the different spans to be different - this will do the job.

0reactions
NathanWalkercommented, May 30, 2017

Just wanted to ping some curiosity here as well - just ran into this tonight 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

bug in angular when data bing inside ngfor loop
The reason that the iShouldrepeatOneTime method is called 4 times is because of Angular's default ChangeDetectionStrategy.
Read more >
ngFor for iterable not working : r/Angular2 - Reddit
The issue is angular's template expression language is not javascript. There's a lot of overlap, and you can do a fair amount, but...
Read more >
NgForOf - Angular
The ngForOf directive is generally used in the shorthand form *ngFor . In this form, the template to be rendered for each iteration...
Read more >
DataGrid does not render columns in the required order when ...
To render ethe columns, I'm iterating through an array that contains the columns using an ngFor, The same ngFor used with this simple...
Read more >
Angular ngFor - Learn All Features, Not Only For Arrays
What is the syntax of ngFor ? · we are passing to ngFor an iteration expression · a loop variable named hero is...
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