angular - Angular 4 Observable subscription can't access integer number to base2[0]
问题描述
In my component I am subscribing Observable method where I am getting a value (value type of number). I can convert it from number to bit using .toString(2) method. I want to access each bit separately. lets say base2= 101010. I want to access it like base2[0] (which wiil be 1).
constructor(private service: serviceApi) {
this.subscription = this.service.getStatus().subscribe(statusCode => {
this.code = statusCode; // console.log(typeof this.code) : number 297
this.base2 = (this.code).toString(2); //console.log(this.base2) : 0100101001
this.base2[1] // Can't to access
});
}
This is throwing error inside observable subscription method (in code above). If I just put same method only inside constructor without subscribes method (by hard coding number), It works like base[1] (1 is the position of binaray number)
解决方案
While using base2 in template try to check if base2 is set using *ngIf
<ng-container *ngIf="base2">
{{ base2[0]}}
</ng-container>
推荐阅读
- nginx - 在插件 'rate-limiting' 中使用 'X-Forwarded-For' 而不是 '$remote_ip'
- java - 尝试在 Head First Servlets 和 JSP 中重现示例时出现 NullPointerException
- c# - 控制台应用程序启动另一个进程环境变量不可访问
- events - READ 和 WRITE 的通用服务 - CQRS + DDD
- c++ - 仅在一定限度内释放 2D 指针数组的内存
- asp.net-mvc - 如何将我的发布方法分配给视图中的按钮
- android - 如何在设计评审中显示标题栏?
- node.js - npm start 时在 react-native 中给出错误
- python - 使用 Python 提取 JSON 文件中的嵌套项
- ionic-framework - 在ionic 3中将API数据从一个页面传递到另一个页面