Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angular – 访问被抄送的内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个组件,我用它来显示一个代码块,该代码块在组件中被转换
<gs-code> console.log("Asd")</gs-code>

该组件看起来像这样

code.component.ts

@Component({
        SELEctor: 'gs-code',providers: [],viewProviders: [],templateUrl: './code.component.html',styleUrls: ['./code.component.less']
    })
    export class GsCodeComponent {
        @input() lang: String;
        @input() currentLang: String;
        @ContentChild('content') content;
        copied(event) {
            console.log(event);
        }

        ngAfterContenTinit() {
            console.log(this.content,"content");
        }
    }

code.component.html

<pre class="prettyprint">
   <ng-content #content></ng-content>
</pre>
<button class="btn btn-sm" title="Copy to clipboard" (click)="copied(content.innerHtml)"><i class="fa fa-clipboard"></i></button>

如何在组件中获取已转换的文本?
我已尝试将contentChild和#content用作< ng-content#content>< / ng-content>.但这些都没有奏效.

< ng-content> element永远不会被添加到DOM本身,因此添加模板变量并查询它不起作用.
你可以包装< ng-content>使用另一个元素并在其中添加模板变量并使用@ViewChild()查询此元素.

然后你可以得到包装元素的innerHTML

@Component({
    SELEctor: 'item',template: '<div #wrapper><ng-content></ng-content></div>'})
class Item implements AfterContenTinit {

    @ViewChild('wrapper') wrapper:ElementRef;
    @input() type:string = "type-goes-here";

    ngAfterContenTinit() {
        console.log(this.wrapper.nativeElement.innerHTML); // or `wrapper.text`
    }
}

另见Get component child as string

大佬总结

以上是大佬教程为你收集整理的angular – 访问被抄送的内容全部内容,希望文章能够帮你解决angular – 访问被抄送的内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。