Angular 改变ngForOf,使其从行为主体获得可接受性

Angular 改变ngForOf,使其从行为主体获得可接受性,angular,directive,angular2-directives,Angular,Directive,Angular2 Directives,对于我的应用程序,我希望有一个类似ngForOf()的指令,但不是从Input()获取iterable对象,而是从指令类中的行为主体获取 在Host类中使用行为主题,如: BehaviourSubject.subscribe((value) => {this.items = value}) <comp ngFor="let item of item"></comp> behavior.subscribe((value)=>{this.items=value})

对于我的应用程序,我希望有一个类似ngForOf()的指令,但不是从Input()获取iterable对象,而是从指令类中的行为主体获取

在Host类中使用行为主题,如:

BehaviourSubject.subscribe((value) => {this.items = value})

<comp ngFor="let item of item"></comp>
behavior.subscribe((value)=>{this.items=value})
这不是一个选择。相反,我想在指令中启动订阅

我想这样使用指令:

 <comp *ngFor></comp>
<comp *forBs="item$"></comp>


有什么办法让这一切顺利吗?(ngForCode的更改)

未经测试,但为了让您了解:

@Directive({
  selector: '[forBs]'
})
export class ForBs<T> extends NgForOf<T> {

  @Input()
  forBs: BehaviorSubject<NgIterable<T>> = new BehaviorSubject([]);

  get ngForOf(): NgIterable<T> {
    return this.forBs.getValue();
  }

  constructor(_viewContainer: ViewContainerRef, _template: TemplateRef<NgForOfContext<T>>, _differs: IterableDiffers) {
    super(_viewContainer, _template, _differs);
  }
}
在哪里

item$ = new BehaviourSubject([]);
不过,您也可以使用
async
pipe;)



你好。感谢您的回答,但仍在使用@Input()。用法应该是@dindunuffin no,不应该是,因为您使用了它前面的
*
。如果您检查
*ngFor
的源代码,您还可以看到@Input在
ngForOf
您想要的
*forBs=“items$”
?我想要并且喜欢构造函数(..){behaviorSubject.subscribe((value)=>this.myIterableObejct=value)},但是您如何才能将主题传递给指令?你需要一些装订
<comp *ngFor="let item of item$ | async"></comp>