Pipe with object (array) -> Change Detection

I use a pipe with an array as argument.

When the array changes the pipe dosn’t update the view.
The solution I found is to set pure: false in the pipe.

Is this the only solution or is there a better way with pipe and objects?

P.S. My pipe make something like this:

Status: John, Maria, Max and four other users are online.

I have read somewhere (i believe angular.io) that complex pipes should be avoided. Instead, put the login in the component

This because of performance

Thanks for you answer Tommertom.

I think peformance is more important than nice readable code in this case. So I put the logic back in my html file.

I think when I put the logic in the ts file I have to manage the change detection of my array by my self.

Use an observable for that

I bet there is one upstream getting the array

I will try both.
I post an image when I’m at home how it looks. It is for a private project.

I use socket.io to inform the client that a new person is logged in on an other client. The client(s) only have to view this information.

Ok. I decided to make it in the html-file. In my case it is ok. But generally the observable woud be the better solution.

         <span *ngIf="ownChatroom.members.length === 0">
            Status: Niemand online
          <span *ngIf="ownChatroom.members.length === 1">
            Status: {{ownChatroom.members[0].displayName}} online
          <span *ngIf="ownChatroom.members.length === 2">
            Status: {{ownChatroom.members[0].displayName}} und {{ownChatroom.members[1].displayName}} online
          <span *ngIf="ownChatroom.members.length === 3">
            Status: {{ownChatroom.members[0].displayName}}, {{ownChatroom.members[1].displayName}} und {{ownChatroom.members[1].displayName}}
          <span *ngIf="ownChatroom.members.length > 3">
            Status: {{ownChatroom.members[0].displayName}}, {{ownChatroom.members[1].displayName}}, {{ownChatroom.members[1].displayName}}
            und {{ownChatroom.members.length -3 }} weitere User online

You may want to take a look at the NgPlural directive found here.
It’s designed for cases like this.

1 Like

Thank you for this hint SigmundFroyd. Now my code looks much nicer :slight_smile:

	<span [ngPlural]="followingChatroom.members.length">
		<ng-template ngPluralCase="=0">
		  Status: Niemand ist online.
		<ng-template ngPluralCase="=1">
		  Status: {{followingChatroom.members[0].displayName}} ist online.
		<ng-template ngPluralCase="=2">
		  Status: {{followingChatroom.members[0].displayName}} und {{followingChatroom.members[1].displayName}} sind online.
		<ng-template ngPluralCase="=3">
		  Status: {{followingChatroom.members[0].displayName}}, {{followingChatroom.members[1].displayName}} und {{followingChatroom.members[2].displayName}}
		  sind online.
		<ng-template ngPluralCase=">3">
		  Status: {{followingChatroom.members[0].displayName}}, {{followingChatroom.members[1].displayName}}, {{followingChatroom.members[1].displayName}}
		  und {{followingChatroom.members.length -3 }} weitere User sind online.

Turns out the ngPlural throws this error when I heva more Users online then I have cases…