代码之家  ›  专栏  ›  技术社区  ›  Davtho1983

条件运算符不适用于ngStyle

  •  3
  • Davtho1983  · 技术社区  · 8 年前

    我有以下代码:

    应用程序。组成部分html

    <button
      [ngStyle]="{
        'backgroundColor': canSave ? 'blue': 'gray',
        'color': canSave ? 'white': 'black',
        'fontWeight': canSave ? 'bold': 'normal'
      }"
    >
      Save
    </button>
    

    应用程序。组成部分输电系统

    import { Component } from '@angular/core';
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      canSave   = 'false';
    }
    

    Save button

    控制台如下所示:

    console

    1 回复  |  直到 8 年前
        1
  •  6
  •   pezetem    8 年前

    canSave应该是布尔值,而不是字符串 这:

     canSave   = false;
    

    不是那样的

     canSave   = 'false';
    
    推荐文章