Angular 访问不是组件或html标记的组件标记的内部文本?
以下是一个例子:Angular 访问不是组件或html标记的组件标记的内部文本?,angular,angular2-template,Angular,Angular2 Template,以下是一个例子: var ListItem = ng.core.Component({ selector: "item", inputs: ["title"], template: "<li>{{title}} | <ng-content></ng-content></li>", }).Class({ constructor: function(){} }) var ListOne = ng.core.Compon
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如何访问标记的innerHTML
我想要一个不使用另一个标记包装内部文本的解决方案。这需要ItemComponent的一些支持: 对于@ContentChildren,我们设置了后代:true:
我希望TypeScript代码仍然有用,我不知道如何在ESx中使用Angular 在es5中,此示例如下所示。很抱歉,但为什么要传递带有后代属性的对象?因为否则只返回直接子对象,而不返回其他后代。因为项目是两级的,所以只有子级才有效。感谢您的ES5 Plunker!
export class ItemComponent {
title: 'title';
constructor(public elementRef:ElementRef){}
}
export class ListoneComponent {
@ContentChildren(ItemComponent, {descendants: true}) list_items;
ngAfterContentInit() {
this.list_items.toArray().forEach((item) => {
console.log(item.elementRef.nativeElement.innerHTML);
})
}
}