1

I have this block of html in my template to show or hide the div.

<div *ngIf="csvVisible">
  <p>Paragraph works</p>
</div>

This is my component.

export class SettingsComponent implements OnInit {

  csvVisible: boolean = false;

  private dataSource: string[];

  @ViewChild(MatTable, { static: true }) table: MatTable<any>;

  constructor(private dialog: MatDialog, private templateParserService: TemplateParserService) { }

  ngOnInit() {
    this.templateParserService.subscribe({
      next(result: string[]) {
        if (result !== null) {
          this.dataSource = result;

          if (this.dataSource && this.dataSource.length) {
            this.csvVisible = true;
          } else {
            this.csvVisible = false;
          }
        }
      },
      error(error: Error) {
        console.log(error.message);
      }
    });
  }

Eventhough the DIV is hidden at start, it doesnt automatically show / hide on the csvVisible value change. Value of csvVisible is properly set when the observer emits data. [hidden]="csvVisible" isn't working either.

Edit :

Subscriber registration on the service is done by the following code.

  private subject = new Subject<string[]>();

  public subscribe(observer: any): Subscription {
    return this.subject.subscribe(observer);
  }

1 Answer 1

1

Since you are using Object inside subscribe, this points to current subscribe object, Instead of using subscribe({next:()}) try using this way

component.ts

 this.templateParserService.subscribe((result: string[])=>{
            if (result !== null) {
                 this.dataSource = result;
               if (this.dataSource && this.dataSource.length) {
                 this.csvVisible = true;
              } else {
                 this.csvVisible = false;
              }
            }
    },(error: Error)=>{
            console.log(error.message);
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This did work without the error function as subscrribe only takes one argument.
I am using a custom subscriber for my service. Its not a HttpClient response but a cusom emitter. I have updated the code including the subscribe method.
try this public subscribe(...observer: any) { return this.subject.subscribe(...observer); }

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.