【问题标题】:ngStyle binding not working when using scope variable使用范围变量时,ngStyle 绑定不起作用
【发布时间】:2018-01-23 02:42:47
【问题描述】:

我有以下组件:

@Component({
  template: `
  <div class="container">
    <div *ngFor="let connection of connections">
      <div class="row">
        <div class='col-2'>{{connection.arrivalTime}}</div>
        <div class='col-1'>{{connection.delay}}</div>
          <div class='col-2'>{{connection.actualArrivalTime}}</div>
          <div class='col-1'>{{connection.icon}}</div>
          <div class='col-1'><span [ngStyle]="{'background-color': connection.colors.bg}">{{connection.line}}</span></div>
          <div class='col-3'>{{connection.direction}}</div>
          <div class='col-2'>{{connection.cancelled}}</div>
        </div>
      </div>
    </div>
styleUrls: ['../app.component.css', '../../simple-grid.css'],
})
export class ZVVComponent {


 connections: PublicConnection[] = [];
  displayDate: Date;

  constructor(private zvvService: ZVVService) {

    this.displayDate = new Date();

   zvvService.getConnections(this.displayDate).subscribe(data => {
      data.forEach( (connection) => {
        this.connections.push(new PublicConnection(
          connection.product.line,
          connection.product.longName,
          connection.product.direction,
          connection.cancelled,
          connection.product.icon,
          connection.product.color,
          connection.mainLocation.time,
          connection.mainLocation.countdown,
          connection.mainLocation.realTime.time,
          connection.mainLocation.realTime.countdown,
          connection.mainLocation.realTime.delay,
          connection.mainLocation.realTime.isDelayed,
          connection.mainLocation.realTime.hasRealTime
        ));
      });
    });
  }
}

如您所见,我在其中一个 div 中使用了 ngStyle,并希望将其绑定到包含颜色的十六进制字符串的变量 connection.colors.bg:

export class Color {
  get fg(): string {
    return this.fg;
  }

  get bg(): string {
    return this.bg;
  }
}

但是,这不起作用,文本保持黑色,背景保持白色。我究竟做错了什么?当我更改它并在其中写入红色而不是变量时,文本显示为红色。

这是 PublicConnection 代码:

从'./color'导入{颜色};

export class PublicConnection {

constructor(
  public line: string,
  private name: string,
  public direction: string,
  public cancelled: boolean,
  public icon: string,
  public colors: Color,
  public arrivalTime: string,
  private countdown: string,
  public actualArrivalTime: string,
  private actualCountdown: string,
  public delay: string,
  private isDelayed: boolean,
  private hasRealtimeData: boolean
) {
  this.direction = this.direction.replace('&#252;', 'ü');
  this.direction = this.direction.replace('&#246;', 'ö');
  this.direction = this.direction.replace('&#252;', 'ü');
}
}

【问题讨论】:

  • 什么是connections
  • 我添加了其余的组件代码,以便您可以看到。

标签: css angular typescript


【解决方案1】:

问题不在于ngStyle 指令——您正确使用了它。很可能是组件首次尝试渲染时未加载数据。

由于您的数据是异步的,我猜在组件渲染和设置背景颜色时,它还没有从服务接收颜色。

通过将模板中的 connection.color.bg 更改为 connection.color?.bg 来尝试使用安全导航运算符。

在此处了解更多信息:https://angular.io/guide/template-syntax#the-safe-navigation-operator----and-null-property-paths

【讨论】:

  • 感谢您的提示。没有改变任何东西。在这种情况下,其他内容不会也不会呈现吗?它加载了相同的调用
  • 没错,无论如何使用安全导航运算符可能是明智的,但我想这不是导致您的问题的原因:/。如果你让 bg getter 总是返回 'red' 会发生什么?
  • 那么它也只是黑色而不是红色。
猜你喜欢
  • 1970-01-01
  • 2012-02-08
  • 2016-02-07
  • 2017-11-10
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
相关资源
最近更新 更多