如何从数组中动态加载离子选项?

时间:2019-01-09 16:30:26

标签: javascript angular ionic-framework ionic3

我有一项加载和保存模板的服务,并且希望在单击离子选择时动态加载选项,以便每个保存的模板都是一个选项,而不是硬编码的选项。

我尝试使用NgFor并创建服务实例来处理JS文件中的模板。

这是HTML代码

```
<ion-item>
    <ion-label>Load template</ion-label>
        <ion-select>
            <ion-option *ngFor = "let template of templates;">
                {{templates}}
            </ion-option>
        </ion-select>
</ion-item>
```

这是模板服务代码

```
export class TemplateService {
    public templates: Template[] = [];
...
getAllTemplates(): Template[] {
    return this.templates;
}
```

这是JS代码

```
export class NewTransactionPage {
    templates: Template[];
...
constructor(public templateServicerino: TemplateService) {
    this.templates = this.templateServicerino.getAllTemplates();
```

我收到一条错误消息,内容为:“未捕获(承诺):错误:StaticInjectorError(AppModule)[NewTransactionPage-> TemplateService:StaticInjectorError(Platform:core)[NewTransactionPage-> TemplateService:NullInjectorError:没有模板服务提供者!”

1 个答案:

答案 0 :(得分:1)

将服务声明为提供者。一种方法是使用这样的注释。

@Injectable({
  providedIn: 'root'
})

export class TemplateService {//body}