代码之家  ›  专栏  ›  技术社区  ›  Hansen

ngIf不在grand child指令中工作

  •  0
  • Hansen  · 技术社区  · 5 年前

    在我使用指令创建孙子之前,在使用指令之前一切都正常 *ngIf 在里面 grandchild 组件,

    我试图找到解决办法,但没有找到解决这个问题的办法。

    这是我的孙子指令

    @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 孙子

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

    即使情况是假的,也能看见人

    我遗漏了什么吗?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Antediluvian    5 年前

    你的孙子是一个指令,但你把它当作一个组件来使用。你应该创建你的孙子作为一个组件。