代码之家  ›  专栏  ›  技术社区  ›  Bailey Miller

角度6*ngfor directive不工作

  •  1
  • Bailey Miller  · 技术社区  · 8 年前

    我刚刚在将cli升级到v6之后创建了一个新项目,并使用angular material util添加了仪表板布局。

    我只是胡闹,意识到ngfor不适合我。

    我确保了commonmodule被导入,我检查了其他指令是否像 *ngIf . 我是做错了什么,还是更新后坏了什么?

    测试组件:

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-test',
      templateUrl: './test.component.html',
      styleUrls: ['./test.component.css']
    })
    export class TestComponent implements OnInit {
    
      topics: ['C','C#']
    
      constructor() { }
    
      ngOnInit() {
      }
    
    }
    

    Html:

    <div>
      <p>Topics</p>
      <ul>
        <li *ngFor="let topic of topics">{{topic}}</li>
      </ul>
    </div>
    

    我刚把我的应用程序测试组件放在仪表板快速启动构建的卡中,下面是呈现的内容。

    截图: enter image description here

    ng—版本: enter image description here

    2 回复  |  直到 8 年前
        1
  •  3
  •   Ramesh Rajendran    8 年前

    已声明变量,但未对其进行初始化。试试这个

    export class TestComponent implements OnInit {
    
        topics: [] = ['C','C#']
    
          constructor() { }
    
          ngOnInit() {
          }
    
        }
    

    说明:

    topics: ['C','C#'] 在这段代码中,typescript是如何处理它的。 ['C','C#'] 是的数据类型 topics

    topics :[]= ['C','C#']

        2
  •  0
  •   manish kumar    8 年前

    您正在声明 话题 而不是初始化它

    尝试

    测试组件

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-test',
      templateUrl: './test.component.html',
      styleUrls: ['./test.component.css']
    })
    export class TestComponent implements OnInit {
    
      topics: string[]
    
      constructor() { 
          this.topics=['C','C#'];
      }
    
      ngOnInit() {}
    
    }