Angular指令:将第一个指令的输出作为第二个指令的模板输入传递
创始人
2024-10-31 00:00:24
0

要将第一个指令的输出作为第二个指令的模板输入传递,可以使用Angular的@Input装饰器和动态组件来实现。以下是一个示例代码:

首先,创建第一个指令,将其命名为FirstDirective:

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[firstDirective]'
})
export class FirstDirective {
  constructor(private elementRef: ElementRef) { }

  getText(): string {
    return this.elementRef.nativeElement.innerText;
  }
}

然后,创建第二个指令,将其命名为SecondDirective,并在其模板中使用第一个指令的输出:

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[secondDirective]'
})
export class SecondDirective {
  @Input() secondDirectiveTemplate: TemplateRef;

  constructor(private viewContainerRef: ViewContainerRef) { }

  ngOnInit() {
    const text = this.secondDirectiveTemplate.createEmbeddedView(null).rootNodes[0].innerText;
    console.log(text); // 输出第一个指令的输出

    // 将第一个指令的输出作为模板输入传递给第二个指令
    this.viewContainerRef.createEmbeddedView(this.secondDirectiveTemplate, { text });
  }
}

最后,在组件的模板中使用这两个指令:

这是第一个指令的输出
{{ text }}

这样,第二个指令就可以获取第一个指令的输出,并将其作为模板输入传递给自己的模板中。

相关内容

热门资讯

辅助透视!aapoker怎么选... 辅助透视!aapoker怎么选牌(透视)透视软件(好像是真的有挂)1、进入游戏-大厅左侧-新手福利-...
透视神器!aapoker发牌逻... 透视神器!aapoker发牌逻辑(透视)插件下载(好像有挂);一、aapoker发牌逻辑软件透明挂的...
透视好友房!aapoker真的... 透视好友房!aapoker真的假的(透视)ai插件(好像是真的有挂);1、在aapoker真的假的a...
透视肯定!aapoker怎么提... 透视肯定!aapoker怎么提高中牌率(透视)辅助插件工具(好像真的是有挂)1、aapoker怎么提...
透视中牌率!aa poker辅... 透视中牌率!aa poker辅助(透视)透视插件(真是是有挂)1、aa poker辅助系统规律教程、...
透视挂透视!aapoker能控... 透视挂透视!aapoker能控制牌吗(透视)透视插件(好像有挂)aapoker能控制牌吗辅助器中分为...
透视透视!aapoker透视插... 透视透视!aapoker透视插件(透视)透视方法(总是存在有挂)1、每一步都需要思考,不同水平的挑战...
透视最新!aapoker发牌逻... 透视最新!aapoker发牌逻辑(透视)ai插件(都是是真的有挂);所有人都在同一条线上,像星星一样...
透视科技!aapoker透视脚... 透视科技!aapoker透视脚本入口(透视)透视方法(竟然真的是有挂)1、首先打开aapoker透视...
透视数据!aapoker插件下... 透视数据!aapoker插件下载(透视)透视脚本下载(竟然是有挂)1、首先打开aapoker插件下载...