【发布时间】:2020-05-10 00:46:44
【问题描述】:
Angular 前端调用必须返回 observable 的服务。 我需要将调用组件的变量设置为服务可观察的值,但我找不到访问组件变量的方法。
希望我没有遗漏一些琐碎的事情,这是我尝试过的。
@Component
metadata
export class AppComponent {
hexResult: string = ''; <-- this needs to be set with data from the service
该服务使用文件读取器读取本地文件。服务有 reader.onLoadend 读取完成时触发。稍微按摩一下数据后,我使用从组件传递到服务的 BehaviorSubject 来发回结果。
但是组件必须订阅 Behavior 主题。然后它可以对结果进行更多处理,但不能将结果返回给组件,然后需要它来影响 html。
这是通过 BehaviorSubject ("binaryRetrieved") 接收数据的组件中的代码:
this.binaryRetrieved.subscribe( (binString) => {
console.log('received raw binary');
if (binString && binString.length > 0) {
this.hexResult = this.binService.binaryToHexString(binString);
}
});
“this.hexResult”未定义,因为它在组件声明中看不到 hexResult。
我迷路了……有什么提示吗?
提前致谢。 修行者
【问题讨论】:
-
你能提供一个plunkr吗?解释有点令人困惑...“我使用 BehaviorSubject 从组件传递到服务来发回结果”是什么意思。
-
好的,我会这样做,但我不知道如何将我将构建的 plunker 连接到这个问题?
-
一旦你建立了你的 plunker,只需将链接添加到你的问题
-
不幸的是,我是 plunker 的新手,所以我去了这里 plnkr.co/edit/tpl:YX7W20?p=preview 添加的内容显示在类的顶部...用 cmets 包裹
标签: javascript angular event-handling filereader subscribe