访问不是组件或 html 标签的组件标签的内部文本?
Access inner text of component tags that is not a component or html tag?
这是一个例子:
var ListItem = ng.core.Component({
selector: "item",
inputs: ["title"],
template: "<li>{{title}} | <ng-content></ng-content></li>",
}).Class({
constructor: function(){}
})
var ListOne = ng.core.Component({
selector: "listone",
queries: {
list_items: new ng.core.ContentChildren(ListItem)
},
directives: [ListItem],
template: "<ul><ng-content select='item'></ng-content></ul>"
}).Class({
constructor: function(){},
ngAfterContentInit: function(){
console.log(this.list_items);
}
})
然后我使用这段代码来显示组件:
<listone>
<item title="first">first</item>
<item title="second">second</item>
</listone>
ListOne
个组件的 ngAfterContentInit
如何访问 <item>
个标签的 innerHTML?
我想要一个不使用另一个标签包裹内部文本的解决方案。
这需要ItemComponent
的一点支持:
export class ItemComponent {
title: 'title';
constructor(public elementRef:ElementRef){}
}
对于 @ContentChildren()
我们设置 descendants: true
:
export class ListoneComponent {
@ContentChildren(ItemComponent, {descendants: true}) list_items;
ngAfterContentInit() {
this.list_items.toArray().forEach((item) => {
console.log(item.elementRef.nativeElement.innerHTML);
})
}
}
我希望 TypeScript 代码仍然有用,我不知道如何在 ESx 中使用 Angular。
这是一个例子:
var ListItem = ng.core.Component({
selector: "item",
inputs: ["title"],
template: "<li>{{title}} | <ng-content></ng-content></li>",
}).Class({
constructor: function(){}
})
var ListOne = ng.core.Component({
selector: "listone",
queries: {
list_items: new ng.core.ContentChildren(ListItem)
},
directives: [ListItem],
template: "<ul><ng-content select='item'></ng-content></ul>"
}).Class({
constructor: function(){},
ngAfterContentInit: function(){
console.log(this.list_items);
}
})
然后我使用这段代码来显示组件:
<listone>
<item title="first">first</item>
<item title="second">second</item>
</listone>
ListOne
个组件的 ngAfterContentInit
如何访问 <item>
个标签的 innerHTML?
我想要一个不使用另一个标签包裹内部文本的解决方案。
这需要ItemComponent
的一点支持:
export class ItemComponent {
title: 'title';
constructor(public elementRef:ElementRef){}
}
对于 @ContentChildren()
我们设置 descendants: true
:
export class ListoneComponent {
@ContentChildren(ItemComponent, {descendants: true}) list_items;
ngAfterContentInit() {
this.list_items.toArray().forEach((item) => {
console.log(item.elementRef.nativeElement.innerHTML);
})
}
}
我希望 TypeScript 代码仍然有用,我不知道如何在 ESx 中使用 Angular。