【发布时间】:2019-10-24 02:32:49
【问题描述】:
在我使用指令创建孙子之前,一切正常,直到我在grandchild 组件中使用指令*ngIf,*ngIf 不起作用。
我试图找到解决方案,但没有找到任何解决这个问题的方法。
这是我的孙子指令指令
@Directive({
selector: 'grand-child-directive ',
})
export class gc{
@Input() name:string;
}
这是我的子组件 ts
@Component({
selector: 'child-component',
templateUrl: './child-component.component.html',
styleUrls: ['./child-component.component.scss'],
})
export class childComponent implements AfterContentInit
@ContentChildren(GrandChildDirective) gc:QueryList<GrandChildDirective> ;
constructor(
) {
}
person:any[] = []
ngAfterContentInit() {
this.gc.toArray().map(item=>{
this.person.push(item)
})
}
}
这是孙子模板
<div *ngFor="let prsn of person">
<a>prsn.name</a>
</div>
这是我调用child和grandchild的模板代码
<child-component>
<grand-child-directive [name]='Ghandy' *ngIf="showGhandy"></grand-child-directive >
<grand-child-directive [name]='Ani' *ngIf="showAni"></grand-child-directive >
<grand-child-directive [name]='Budi' *ngIf="showBudi"></grand-child-directive >
</child-component>
这是我的 ts
export class testComponent implements OnInit {
constructor() {
}
showGhandy= true
showAni= true
showBudi:boolean = true
ngOnInit(){
if(someCondition){
this.showGhandy = false
}
if(someCondition){
this.showAni= false
}
if(someCondition){
this.showBudi= false
}
}
}
即使条件为假,人物仍然可见
我错过了什么吗?
我很乐意提供任何帮助。
【问题讨论】:
-
我认为您的代码没有问题。你能重现你的问题吗?
-
@zmag :正如你所说,我也对我的问题感到困惑.. 我的代码没有问题.. 实际上我的代码中有一些问题
-
你的
grand-child-directive是指令,应该没有模板。这让我很困惑,你能提供一个stackblitz.com 的复制品吗?