angular - Angular 模板表达式中的按位运算
问题描述
我正在创建一个代表星期几的组件。我正在尝试使用按位算术来确定是否已经选择了某一天(可能这属于编码高尔夫?)。
然而,Angular 编译器抱怨&
符号不应该出现在模板表达式中。我究竟做错了什么?我已经尝试用 替换&
,&
并且还发现了一些处理此问题的问题,但|
仅适用于 (OR) 操作。
提前致谢。
天-run.component.html
<div>
<p>Filter on days run</p>
<table>
<tr>
<td (click)='click(1)' [class.selected]='daysRun & 1'>M</td>
<td (click)='click(2)' [class.selected]='daysRun & 2'>T</td>
<td (click)='click(3)' [class.selected]='daysRun & 4'>W</td>
<td (click)='click(4)' [class.selected]='daysRun & 8'>H</td>
<td (click)='click(5)' [class.selected]='daysRun & 16'>F</td>
<td (click)='click(6)' [class.selected]='daysRun & 32'>S</td>
<td (click)='click(7)' [class.selected]='daysRun & 64'>U</td>
<td> {{ daysRun }} </td>
</tr>
</table>
</div>
天运行.component.ts
import { Component, OnInit } from '@angular/core';
import { ModelService } from './../model/model.service'
@Component({
selector: 'app-days-run',
templateUrl: './days-run.component.html',
styleUrls: ['../controlsCommon.css','./days-run.component.css'],
providers: [ModelService]
})
export class DaysRunComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
// 1 = Monday, 2 = Tuesday, 4 = Weds, 8 = Thurs, 16 = Fri, 32 = Sat, 64 = Sun
daysRun = 0;
daysRunString = "MO";
click(daysRun : number) : void {
// Toggles bit 0 - 6 using XOR (^) operator
this.daysRun ^ (Math.pow(2, daysRun - 1));
}
}
解决方案
推荐阅读
- css - css 网格网格模板列:
- mysql - MySQL 150:外键约束的格式不正确
- android - 如何为两个不同的项目生成 OAuth 客户端 ID 的 SHA1 指纹?
- python - 如何更改python字典的值
- python - Tensorflow 会话运行不适用于 mean_squared_error
- html - 如何使滑块无限?
- java - 请求处理失败;嵌套异常是 org.hibernate.QueryException:
- android - Android 构建错误 [app:processDebugManifest]
- php - 是否有必要将PDO放在IF语句中?
- php - 在会话中存储数据与将数据发送到数据库中