简体   繁体   English

自定义元素(聚合物)属性绑定到 RxJS 可在 Angular 中观察到 9

[英]Custom element(Polymer) property binding to an RxJS Observable in Angular 9

I have an Angular 9 application where Angular Component properties binds to RxJS observables.我有一个 Angular 9 应用程序,其中 Angular 组件属性绑定到 RxJS 可观察对象。 Here I am trying to replace Angular Components with Polymer custom element (paper-input).在这里,我试图用 Polymer 自定义元素(纸张输入)替换 Angular 组件。

What is my observation?我的观察是什么? I am unable to bind custom element property with RxJS observables which I was able to with Angular Component properties.我无法将自定义元素属性与 RxJS 可观察对象绑定,而我可以使用 Angular 组件属性来绑定。

How to reproduce?如何重现? For showing what I am facing I have stackblitz sample with custom-element(paper-input) binds to an observable.为了展示我所面临的情况,我将带有自定义元素(纸张输入)的 stackblitz 示例绑定到一个可观察对象。

app.component.html app.component.html

<paper-input [label]="price$ | async" ></paper-input>

app.component.ts app.component.ts

import { Observable } from 'rxjs';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent  {
  price: Observable<string>;
  
  constructor() {
      this.price = Observable.of("230$");
  }
}

Full Code: https://stackblitz.com/edit/angular-examples-mu72vs完整代码: https://stackblitz.com/edit/angular-examples-mu72vs

Expectation is I am able to bind the label property of paper-input (Polymer) to Observable and when ever there is a change gets reflected to custom element property.期望我能够将纸张输入(Polymer)的 label 属性绑定到Observable ,并且每当有更改时都会反映到自定义元素属性。 In actual I am not seeing the changes reflected to label property and it is empty.实际上,我没有看到反映到 label 属性的更改,它是空的。

Here I assumed the observable subscription will happen as part of AsyncPipe using while binding.在这里,我假设 observable 订阅将作为 AsyncPipe 的一部分使用 while 绑定发生。 But that is not happening with a custom element property.但是自定义元素属性不会发生这种情况。

What is the best way to achieve custom element property binding with RxJS Observables?使用 RxJS Observables 实现自定义元素属性绑定的最佳方法是什么?

Regards Basanth问候巴桑特

Now it is working, there was a minor mistake from my Angular code.现在它正在工作,我的 Angular 代码有一个小错误。 Basically the way I declared and used the observable.基本上是我声明和使用 observable 的方式。 We should have '$' sign used for indicating it as a observable variable.我们应该使用“$”符号将其指示为可观察变量。

ie with the below change it is working fine.即通过以下更改,它工作正常。

export class AppComponent  {
  price$: Observable<string>;
  
  constructor() {
      this.price$ = Observable.of("230$");
  }
}

I doubted aync pipe with CustomELement, but there is no problem using async pipe with Polymer Custom ELements.我怀疑 aync pipe 和 CustomELement,但是使用 async pipe 和 Polymer 自定义元素没有问题。

You can find the working sample here @ https://stackblitz.com/edit/angular-examples-udy8qe你可以在这里找到工作样本@ https://stackblitz.com/edit/angular-examples-udy8qe

Thanks谢谢

Basanth巴桑特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM