Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 访问不是组件或html标记的组件标记的内部文本?_Angular_Angular2 Template - Fatal编程技术网

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);
    })
  }
}