A
A
Alexander Belov2017-04-26 01:26:32
Angular
Alexander Belov, 2017-04-26 01:26:32

How to fix in Angular2 Cannot read property 'value' of undefined?

I can't figure out where the error is.
There is a component with the following code:

<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>

And this code in the 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);
  }

}

The point is to (click)="onSelect(release)remove the block with the class class="releasesMenu".
I *ngIf="detailedInfo"try, but I keep getting errors like Cannot read property 'value' of undefined .
What is the problem and how to get rid of this error - I can not understand, please help, who fumbles.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Lopatin, 2017-04-26
@lorus

You have an error in another expression. Namely in

search.value
. search variable not in scope

O
ozknemoy, 2017-04-29
@ozknemoy

the simplest is to make search optional in the markup
*ngFor="let release of (releases| searchFilter: search?.value )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question