使用异步管道订阅对象的属性
以这种方式订阅对象的属性有什么缺点吗:
<th [attr.colspan]="(headerColSpan$ | async).aaa">...</th>
<th [attr.colspan]="(headerColSpan$ | async).bbb" >...</th>
<th [attr.colspan]="(headerColSpan$ | async).ccc">...</th>
<th [attr.colspan]="(headerColSpan$ | async).ddd" >...</th>
this.headerColSpan$ = new Observable((subscriber) => {
subscriber.next(this.headerColspan);
});
this.headerColspan = {aaa: 2, bbb: 3, ccc: 1, ddd: 5};
这似乎是如何工作的,但我不确定它在如何使用 RxJS 的意义上是否正确。
另一种方法是创建四个BehaviorSubjects。每个 colspan 一个。
出于某种原因,这样会更好吗?
Are there any downsides to subscribe to properties of an object this way:
<th [attr.colspan]="(headerColSpan$ | async).aaa">...</th>
<th [attr.colspan]="(headerColSpan$ | async).bbb" >...</th>
<th [attr.colspan]="(headerColSpan$ | async).ccc">...</th>
<th [attr.colspan]="(headerColSpan$ | async).ddd" >...</th>
this.headerColSpan$ = new Observable((subscriber) => {
subscriber.next(this.headerColspan);
});
this.headerColspan = {aaa: 2, bbb: 3, ccc: 1, ddd: 5};
This is how it seems to work, but I'm not sure if it's correct in the sense of how RxJS should be used.
The alternative would be to make four BehaviorSubjects. One for each colspan.
Would that be better for any reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
async
管道一次,并将其结果分配给模板变量,然后使用它来访问您需要的属性,如下所示:You can use the
async
pipe once, and assign its result to a template variable, then use it to access the properties you need, like the following: