【发布时间】:2022-02-05 00:12:54
【问题描述】:
我正在尝试在列表中显示其方法“arduino”等于 true(它是一个布尔值)的项目。
我尝试在同一行上执行 *ngFor 和 *ngIf 但它给出了错误,所以当在另一行上执行时,系统也会加载方法“arduino”等于“false”的项目,所以它效果不好
如何从 component.ts 中过滤它,以便只加载具有 project.arduino = true 方法的项目?
这是component.ts中的代码
@Component({
selector: 'app-arduino-projects',
templateUrl: './arduino-projects.component.html',
styleUrls: ['./arduino-projects.component.css'],
providers: [ProjectService]
})
export class ArduinoProjectsComponent implements OnInit {
public projects: Project[];
public url: string;
constructor(
private _projectService: ProjectService
) {
this.projects = [];
this.url = Global.url;
}
ngOnInit(): void {
this.getProjects();
}
getProjects(){
this._projectService.getProjects().subscribe(
response => {
if(response.projects){
this.projects = response.projects; **************** this is the property that I have to filter and I don't know how to do it
}
},
error => {
console.log(error)
}
)
}
这是component.html中的相关代码
<div class="project-list">
<ul>
<li *ngFor="let project of projects" class="project">
<ng-container *ngIf="project.arduino == true"> ************ Here i filtered it in that way
<a [routerLink]="['/project', project._id]">
<div class="image-box">
<div class="image">
<img src="{{ url+'get-image/'+project.imagefront }}" *ngIf="project.imagefront" />
</div>
</div>
</a>
</ng-container>
</li>
</ul>
</div>
通过这种方式,它正在加载 project.arduino = false 的项目,它不会在屏幕上显示它们,但会弄乱浏览器中的列表
我正在使用 Angular 13
非常感谢!
【问题讨论】:
标签: javascript html angular typescript pipe