gpt4 book ai didi

angular - 无法读取 Angular2 Typescript 中未定义的属性 'value'

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:38 25 4
gpt4 key购买 nike

我正在构建一个 Angular2 应用程序,并试图通过 *ngIf

摆脱点击时的 div

这是我的观点:

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

这是我的 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);
}

}

我想要的是在 (click)="onSelect(release) 上删除/隐藏我的 .releasesMenu block 。但我收到错误:无法读取未定义的属性“值”

最佳答案

那是因为 #search*ngIf

将其添加到您的组件类中

@ViewChild('search')search:ElementRef;

并将*ngFor更改为

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

另见 Angular2, *ngIf and local template variables

关于angular - 无法读取 Angular2 Typescript 中未定义的属性 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43640861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com