2

I am building an Angular2 app and trying to get rid of a div on click through *ngIf

Here's my view:

<router-outlet>

<div class="releasesMenu" *ngIf="detailedInfo">
  <h1>Releases</h1>
  <div class="search">
    <input type="text" #search (keyup)="0" placeholder="Find release by its name, song or year">
  </div>
</div>

<div class="releasesOutlet container">
        <div class="row releasesBlock">
           <div *ngFor="let release of (releases| searchFilter: search.value )" class="releaseRender" (click)="onSelect(release)">
              <div class="releaseRenderWrap" *ngIf="!selectedRelease">
            <span>Something interesting...</span>
          </div>
       </div>

        <div class="releaseDetails" *ngIf="selectedRelease">
          <div class="releaseDetailsWrap">
                <span>Some details...</span>
          </div>
        </div>
      </div>
  </div>
</router-outlet>

Here's my controller:

import { Component, OnInit } from '@angular/core';
import { SearchFilter } from './../search-filter.pipe';
import { ReleasesService } from '../releases/releases.service';

@Component({
  selector: 'releases-component',
  providers: [ReleasesService],
  templateUrl: './app/releases/releases.component.html',
  styleUrls: ['./app/releases/releases.component.css','./css/grid.css']
})

export class ReleasesComponent implements OnInit { 
  releases: any = [];
  release: any;
  selectedRelease: any;
  private detailedInfo: boolean;

  onSelect(release){
    this.detailedInfo = false;
    this.selectedRelease = release;
    console.log(release)
    }

  constructor(private releasesService: ReleasesService){ }

  ngOnInit(){
    this.releasesService.getReleases()
    .subscribe(data => this.releases = data);
  }

}

What I want is to remove / hide my .releasesMenu block on (click)="onSelect(release). But I am getting the error: Cannot read property 'value' of undefined

1 Answer 1

3

That's because #search is inside an *ngIf

Add this to your components class

@ViewChild('search')search:ElementRef;

and change *ngFor to

*ngFor="let release of (releases| searchFilter: search?.nativeElement?.value )"

See also Angular2, *ngIf and local template variables

Sign up to request clarification or add additional context in comments.

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.