0

I need help with on click function in Angular 4. I've got such an element:

      <mat-cell *matCellDef="let row">
        <span>{{row.messageText | hasString}}</span>
        <span *ngIf="row.messageText && row.messageText.length >= 30"><span class="show-more" (click)="showMore(row)">more</span></span>
      </mat-cell>

I have a string in table cell from row.messageText, which is cut into shorter string by *ngIf directive. If length of the string is too much, I shorten it by substr() function. and display in the table cell. I also add a "more" span to it so I can click on it and expand this string.

And there is my problem - what can I do so I can click on "more" and show entire length of this string? My idea is to bind on click function to span "show more" and return a full string.

This is the function in my component .ts file:

showMore(row) {
  return row.messageText.substr();
}

The function returns full string. How can I apply this to my component.html?

2 Answers 2

1

You can call the function within the expression

<span>{{showMore(row)}}</span>
Sign up to request clarification or add additional context in comments.

Comments

1

You can send custom parameter showFullString to your hasString pipe.

@Pipe({name: 'hasString'})
export class hasString implements PipeTransform {
    /**
     *
     * @param value
     * @returns {String}
     */
    transform(value: String, showFullString:Boolean): String {
        if(showFullString) {
          return value;
        }else {
          return value.substr() ; //substring as per your requirement.
        } 
    }
}

This showFullString Boolean can be toggled from showMore method :

showMore(row) {
  this.showFullString = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.