AngularJS Typescript通过appsettings JSON循环

时间:2018-08-31 19:09:36

标签: angularjs typescript

我很难遍历我拥有的appsettings.json文件的特定部分。我正在使用带有typescript的angularjs的任何帮助,将不胜感激。

ServerConfig.ts

export interface ServerConfig {  
  campaigns: Campaign[];
}

export interface Campaign {
  title: string;
  description: string;
  enabled: boolean;
}

Appsettings.json

{
  "Settings": {    
    "Campaigns": [
      {
        "Title": "Test1",
        "Description": "This is test 1",
        "Enabled": true
      },
      {
        "Title": "Test2",
        "Description": "This is test 2",
        "Enabled": true
      }
    ],    
  },
}

打字稿

import { ServerConfig } from "../core/ServerConfig";

declare var config: ServerConfig;

export class IndexController {
  constructor(
  ) { }

  public $onInit() {

    var campaigns = config.campaigns;

    campaigns.forEach(function (data) {
      console.log(data.title);
    });

  }
}
export var home: ng.IComponentOptions = {
  controller: IndexController,
  templateUrl: "/app/index.html"
};

我得到的错误是“ campaigns.forEach不是函数”

1 个答案:

答案 0 :(得分:0)

我所看到的问题是我没有将其正确设置为数组。

ServerConfig.ts

export interface ServerConfig {
  campaigns: Campaign;
}

interface CampaignItem {
  title: string;
  description: string;
  enabled: boolean
}

interface Campaign extends Array<CampaignItem> { }