首页 > 解决方案 > 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));
  }
}

标签: angulartypescriptangular-templatebitwise-andbitwise-xor

解决方案


推荐阅读