【问题标题】:Angular 6 *ngSwitchCase Not WorkingAngular 6 *ngSwitchCase 不工作
【发布时间】:2018-12-18 17:25:18
【问题描述】:

示例:我正在使用 div 容器,当我运行代码时,我只得到什么 是开关的默认值。

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-test',
template: `  
          <div [ngSwitch]="toy_color">
             <div *ngSwitchCase="'red'"> RED </div>
             <div *ngSwitchCase="'blue'">  BLUE </div>
             <div *ngSwitchCase="'Yellow'"> YEllOW </div>
             <div *ngSwitchDefault> Pick Again </div>
          </div>

          `
styles: []
})
export class TestComponent implements OnInit {

 public toy_color:"red";

constructor() { }

ngOnInit() { }
}

【问题讨论】:

  • 尝试编辑您的帖子以澄清您的问题。

标签: angular6


【解决方案1】:

您的代码下面一行不正确。

public toy_color:"red";

您没有为 toy_color 赋值。您应该使用它的类型更改代码,或者您可以直接为变量赋值,如下所示。

  1. 有分配类型

public toy_color: string = "red";

  1. 直接赋值

public toy_color = "red";

我创建了一个demo 供您参考。

【讨论】:

  • 很高兴听到它解决了您的问题。你能投票给答案吗?
【解决方案2】:

您的变量toy_color 未定义。将其更改为以下行:

public toy_color: string = "red";

public toy_color = "red";

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 2018-11-25
    • 2019-01-24
    • 1970-01-01
    • 2019-01-29
    • 2019-01-07
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多