为什么我不能从Typescript中的方法动态设置样式

为什么我不能从Typescript中的方法动态设置样式,typescript,binding,styles,Typescript,Binding,Styles,我试图使用.ts文件中的方法设置表中的行样式,但出现以下错误: > IndexComponent.html:16 ERROR Error: Cannot find a differ supporting > object 'cursor:pointer' > at KeyValueDiffers.push../node_modules/@angular/core/fesm5/core.js.KeyValueDiffers.find > (core.js:16533

我试图使用
.ts
文件中的方法设置表中的行样式,但出现以下错误:

> IndexComponent.html:16 ERROR Error: Cannot find a differ supporting
> object 'cursor:pointer'
>     at KeyValueDiffers.push../node_modules/@angular/core/fesm5/core.js.KeyValueDiffers.find
> (core.js:16533)
>     at NgStyle.set [as ngStyle] (common.js:3816)
>     at updateProp (core.js:18743)
>     at checkAndUpdateDirectiveInline (core.js:18494)
>     at checkAndUpdateNodeInline (core.js:19801)
>     at checkAndUpdateNode (core.js:19763)
>     at debugCheckAndUpdateNode (core.js:20397)
>     at debugCheckDirectivesFn (core.js:20357)
>     at Object.eval [as updateDirectives] (IndexComponent.html:16)
>     at Object.debugUpdateDirectives [as updateDirectives] (core.js:20349)
HTML

<table class="table">
      <tr>
        <th>Id</th>
        <th>Name</th>
      </tr>

      <tr [ngStyle]="setStyle(item)" (mousedown)="onSelectionChanged(item)" *ngFor="let item of viewComputers">
        <td>{{item.id}}</td>
        <td>{{item.username}}</td>
      </tr>
    </table>

正如您所看到的,如果表上没有选择(没有将逻辑放在这里),或者如果当前行未被选择,我希望设置样式,否则我希望指定一行。

ngStyle需要对象,而不是字符串

代码更改

  public setStyle(value:Computer):any{

    let style: any ={'cursor': 'pointer'};
    if(this.SelectedComputer==null ||!(this.SelectedComputer.id==value.id) ){
      console.log(style);
      return style;
    }
    return {...style, 'background-color' : '#6699ff' };
  }

ngStyle需要对象,但不需要字符串

代码更改

  public setStyle(value:Computer):any{

    let style: any ={'cursor': 'pointer'};
    if(this.SelectedComputer==null ||!(this.SelectedComputer.id==value.id) ){
      console.log(style);
      return style;
    }
    return {...style, 'background-color' : '#6699ff' };
  }

ngStyle
指令正在引发异常,它希望将您设置为差异的值解释为异常。要设置原始样式,您需要绑定到
style
而不是
ngstyle
,或者在quotesOk i bound
style
中包装它,而不是
ngstyle
,现在表格已呈现,但按下时的颜色仍然不起作用;我在每一行得到警告:
core.js:11407警告:清理不安全的样式值游标:指针(请参阅http://g.co/ng/security#xss)
ngStyle
指令正在引发异常,它希望将您设置的值解释为不同的值。要设置原始样式,您需要绑定到
style
而不是
ngstyle
,或者在quotesOk i bound
style
中包装它,而不是
ngstyle
,现在表格已呈现,但按下时的颜色仍然不起作用;我在每一行得到警告:
core.js:11407警告:清理不安全的样式值游标:指针(请参阅http://g.co/ng/security#xss)
  public setStyle(value:Computer):any{

    let style: any ={'cursor': 'pointer'};
    if(this.SelectedComputer==null ||!(this.SelectedComputer.id==value.id) ){
      console.log(style);
      return style;
    }
    return {...style, 'background-color' : '#6699ff' };
  }