AngularJS Typescript loop through appsettings JSON
AngularJS Typescript loop through appsettings JSON
im having a really hard time looping through a specific section of the appsettings.json file i have. I am using angularjs with typescript any help would be greatly appreciated.
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
],
,
Typescript
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"
;
The error I get is "campaigns.forEach is not a function"
campaigns : description : null enabled : false title : null
campaigns:title: null, description: null, enabled: false to be more specific I dont think it picking up the inner part of campaigns as an array– EugeneDW
Aug 31 at 19:17
campaigns:title: null, description: null, enabled: false
yes that is obious because you have not assigned, why dont you use http.get and bind?
– Sajeetharan
Aug 31 at 19:56
1 Answer
1
The problem from what I can see is I didn't set it up correctly as an array.
ServerConfig.ts
export interface ServerConfig
campaigns: Campaign;
interface CampaignItem
title: string;
description: string;
enabled: boolean
interface Campaign extends Array<CampaignItem>
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
campaigns : description : null enabled : false title : null@Sajeetharan thats odd :/– EugeneDW
Aug 31 at 19:15