【问题标题】:Why NG-REPEAT doesn't work with json variable from service?为什么 NG-REPEAT 不适用于服务中的 json 变量?
【发布时间】:2018-08-11 05:01:34
【问题描述】:

我的 StudentService.ts 中有一个 JSON 变量,我想从这些 JSON 中填充我的选择选项。

我的服务:

  careers : {};
  constructor(private http: HttpClient) { 
    this.selectedStudent = new Student();
    this.careers = [
      {"id":"itic", "name":"Sistemas"},
      {"id":"itic", "name":"Sistemas"},
      {"id":"itic", "name":"Sistemas"},
    ];
  }

我的模板.html

<p>{{ studentService.careers | json }}</p>
<div class="input-field col s12">
  <select>
    <option ng-repeat="career in studentService.careers">{{career.name}}</option>
  </select>
    <label>Materialize Select</label>
</div>  

template.html 中的第一行有效,并显示了我的 JSON,但我无法在我的选择中复制它。

【问题讨论】:

标签: json angular


【解决方案1】:

Angular没有ng-repeat,对应的语法是ngFor,需要用ngFor改一下,

<select>
    <option *ngFor="let career of studentService.careers">{{career.name}}</option>
</select>

【讨论】:

  • 在提出问题之前,我尝试了该方法,但没有奏效。经过一夜,我意识到这是一个 Materialize CSS 框架错误,显然是“M.AutoInit();”不会自动初始化所​​有元素,选择其中之一。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多