如果某个变量是null
,我想隐藏一个div,但是即使我直接赋值true,*ngIf
也不起作用。tree.component.ts
import { Component, OnInit } from '@angular/core';
import { Tree} from '../model/tree';
import { TreeService } from '../service/tree.service';
@Component({
selector: 'app-tree',
templateUrl: './tree.component.html',
styleUrls: ['./tree.component.css']
})
export class TreeComponent implements OnInit {
canShow: boolean = true;
tree!: Tree;
constructor(private treeService:TreeService) {
}
ngOnInit(): void {
this.tree= this.treeService.returnTree();
}
}
tree.component.html
...
<div *ngIf="canShow">
<h1 class="sm:text-2xl font-medium mb-2 text-gray-900">Significado dos nomes populares</h1>
<p class="pb-3 leading-relaxed text-gray-500">{{tree.nameMeaning}}</p>
</div>
...
tree.component.html
...
<div *ngIf="true">
<h1 class="sm:text-2xl font-medium mb-2 text-gray-900">Significado dos nomes populares</h1>
<p class="pb-3 leading-relaxed text-gray-500">{{tree.nameMeaning}}</p>
</div>
...
控制台出错:
NG0303: Can't bind to 'ngIf' since it isn't a known property of 'h2'.
psyklopz在link上的解决方案
我已经将TreeComponent
导入app.module.ts
的@NgModule
declarations
。
1条答案
按热度按时间ig9co6j11#
ngIf
需要Angular的CommonModule将
CommonModule
安装到组件的父模块中,因为它具有ngIf
指令。我在你的例子中没有看到这一点。