【问题标题】:Unable to access child structural directive from parent structural directive无法从父结构指令访问子结构指令
【发布时间】:2017-10-15 12:45:27
【问题描述】:

我正在尝试从父指令访问子结构指令。

这就是我试图访问孩子的方式

@Directive({
  selector: '[appChildStruralDirective]'
})
export class ChildStruralDirective {
  constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private renderer: Renderer) {
    this.viewContainer.createEmbeddedView(this.templateRef);
  }

  ngAfterViewInit(): void {
  }
}

@Directive({
  selector: '[appParentStruralDirective]'
})
export class ParentStruralDirective {
  @ViewChild(ChildStruralDirective) viewChild: ChildStruralDirective;
  @ContentChild(ChildStruralDirective) contentChild: ChildStruralDirective;

  constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private renderer: Renderer) {
    this.viewContainer.createEmbeddedView(this.templateRef);
  }

  ngAfterViewInit(): void {
    console.log('ngAfterViewInit:viewChild', this.viewChild);
    console.log('ngAfterViewInit:contentChild', this.contentChild);
  }

  ngAfterContentChecked(): void {
    console.log('ngAfterContentChecked:viewChild', this.viewChild);
    console.log('ngAfterContentChecked:contentChild', this.contentChild);
  }
}
@Component({
  selector: 'my-app',
  template: `
   <h1 *appParentStruralDirective>
     <span *appChildStruralDirective>Hello world</span>
   </h1>
  `,
})
export class App {
  constructor() {
  }
}

我不确定为什么子指令不可访问。

我创建了一个 plunkr 来演示这个问题。

https://plnkr.co/edit/deQC6cY4oHuNdKbzsfhE?p=preview

请告诉我为什么这不起作用。

PS:我同时使用了 ViewChild 和 ContentChild(很可能是候选),因为我不确定这属于哪一个,因为它们是动态生成的元素。

【问题讨论】:

标签: angular angular2-directives angular2-di


【解决方案1】:

您可以为每个父子组合创建一个服务。

这里是 plunker:https://plnkr.co/edit/zNf7iZtOQtlsSfYOfq5D?p=preview

  @Injectable()
  export class MyState{
    id;
    child;
  }

  @Directive({
    selector: '[appChildStruralDirective]'
  })
  export class ChildStruralDirective {
    random;

    constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private myState : MyState) {
      this.viewContainer.createEmbeddedView(this.templateRef);
     this.myState.child = this;
     this.random = Math.random();
    }

    ngAfterViewInit(): void {
    }
  }

  @Directive({
    selector: '[appParentStruralDirective]'
  })
  export class ParentStruralDirective {
    child;

    constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, private renderer: Renderer,
    private s : MyState) {
      this.viewContainer.createEmbeddedView(this.templateRef);
    }
  }

  @Component({
    selector: 'my-comp',
    providers: [MyState],
    template: `
    <fieldset style="margin-top:50px">
       <button (click)="showMyState()">Show My State</button>
       <h1 *appParentStruralDirective>
         <span *appChildStruralDirective>Hello world {{id}}</span>
       </h1>
     </fieldset>
    `,
  })
  export class MyCmp {
    @Input() id: string;
    constructor(private myState: MyState) {
    }

    ngOnInit(){
      this.myState.id=this.id;
    }

    showMyState() {
      console.log(this.myState);
    }
  }

  @Component({
    selector: 'my-app',
    template: `
        <my-comp [id]="'one'"></my-comp>
        <my-comp [id]="'two'"></my-comp>
    `,
  })
  export class App {
    constructor() {
    }
  }

【讨论】:

  • 感谢您的支持。如果我在上述组件的模板中有多个appParentStruralDirective 实例,那么当我说我们需要每个服务有多个唯一键来引用父子组合的每个实例时,我所指的是同一件事。 (这恰好是我的要求)但是,这应该作为原生角度解决方案可用之前的第一步。我需要允许单个只读字段在任意组件上内联可编辑。类似于 JIRA hibernate.atlassian.net/browse/HHH-11731
猜你喜欢
  • 2019-12-16
  • 2021-03-10
  • 2020-06-08
  • 1970-01-01
  • 2018-05-29
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多