angular - changing data in assets folder of the build Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to copy files from 'assets' folder to sdcard?Using fonts with Rails asset pipelineCheck if assets changed between app reinstall (Android)Struggling with assets foldersDeploying web app: updating assets on cdn and references to assets on serverWinphone8.1 development: Can I get a List of al files stored within the Assets folder?Grunt - change asset linking when building for productionAngular HTML bindingHow to detect a route change in Angular?Adding a bunch of assets to a Xamarin Android project without copying

What is the meaning of the simile “quick as silk”?

Would "destroying" Wurmcoil Engine prevent its tokens from being created?

Can anything be seen from the center of the Boötes void? How dark would it be?

Is safe to use va_start macro with this as parameter?

Generate an RGB colour grid

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Is there any way for the UK Prime Minister to make a motion directly dependent on Government confidence?

What is homebrew?

Fundamental Solution of the Pell Equation

Find the length x such that the two distances in the triangle are the same

An adverb for when you're not exaggerating

Trademark violation for app?

Closed form of recurrent arithmetic series summation

Quick way to create a symlink?

Extracting terms with certain heads in a function

Why do the resolve message appear first?

Crossing US/Canada Border for less than 24 hours

Do I really need recursive chmod to restrict access to a folder?

How would a mousetrap for use in space work?

Do square wave exist?

Can melee weapons be used to deliver Contact Poisons?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

What is the longest distance a player character can jump in one leap?

Is it fair for a professor to grade us on the possession of past papers?



angular - changing data in assets folder of the build



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to copy files from 'assets' folder to sdcard?Using fonts with Rails asset pipelineCheck if assets changed between app reinstall (Android)Struggling with assets foldersDeploying web app: updating assets on cdn and references to assets on serverWinphone8.1 development: Can I get a List of al files stored within the Assets folder?Grunt - change asset linking when building for productionAngular HTML bindingHow to detect a route change in Angular?Adding a bunch of assets to a Xamarin Android project without copying



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want my angular app to work as a Standalone without a server.



In the assets folder are images, sound-files etc. and one game.json file which contains the business data for the application (On a server this data would be stored in a DB or would be called via Rest).



Everything works quite well. Using 'ng build' I can create the complete build which can be startet by opening the index.html in my OS - file manager without a http-Server.



The problem is that I can't change the content of game.json after the build, because it seems to be included in the build in some way. Changes are ignored, it freezes the content in state it was compiled.



Since I will run the app only locally I read my data this way:



typings.d.ts:



declare module "*.json" 
const value: any;
export default value;



game.service.ts:



import * as data_json from 'assets/data/game.json';

@Injectable()
export class GameService

constructor()
loadGames(): Observable<Game>
return Observable.of(JSON.parse(JSON.stringify(data_json)));




I don't use HTTPClient because I would run into CORS problems.
Reading the data works fine, but changes in the file game.json are ignored completely.



Is there a way to make this file "modifyable" so I can change it in the assets folder of the build?










share|improve this question






















  • Why you don't want to use HTTPClient to get data from assets folder? I don't think you will have any issue related to CORS if you it.

    – Trimantra Software Solution
    Mar 22 at 9:48











  • I tried to, but I ran into CORS issues.

    – Otis Ottington
    Mar 22 at 9:50











  • You can try my solution mentioned below.

    – Trimantra Software Solution
    Mar 22 at 9:59

















0















I want my angular app to work as a Standalone without a server.



In the assets folder are images, sound-files etc. and one game.json file which contains the business data for the application (On a server this data would be stored in a DB or would be called via Rest).



Everything works quite well. Using 'ng build' I can create the complete build which can be startet by opening the index.html in my OS - file manager without a http-Server.



The problem is that I can't change the content of game.json after the build, because it seems to be included in the build in some way. Changes are ignored, it freezes the content in state it was compiled.



Since I will run the app only locally I read my data this way:



typings.d.ts:



declare module "*.json" 
const value: any;
export default value;



game.service.ts:



import * as data_json from 'assets/data/game.json';

@Injectable()
export class GameService

constructor()
loadGames(): Observable<Game>
return Observable.of(JSON.parse(JSON.stringify(data_json)));




I don't use HTTPClient because I would run into CORS problems.
Reading the data works fine, but changes in the file game.json are ignored completely.



Is there a way to make this file "modifyable" so I can change it in the assets folder of the build?










share|improve this question






















  • Why you don't want to use HTTPClient to get data from assets folder? I don't think you will have any issue related to CORS if you it.

    – Trimantra Software Solution
    Mar 22 at 9:48











  • I tried to, but I ran into CORS issues.

    – Otis Ottington
    Mar 22 at 9:50











  • You can try my solution mentioned below.

    – Trimantra Software Solution
    Mar 22 at 9:59













0












0








0








I want my angular app to work as a Standalone without a server.



In the assets folder are images, sound-files etc. and one game.json file which contains the business data for the application (On a server this data would be stored in a DB or would be called via Rest).



Everything works quite well. Using 'ng build' I can create the complete build which can be startet by opening the index.html in my OS - file manager without a http-Server.



The problem is that I can't change the content of game.json after the build, because it seems to be included in the build in some way. Changes are ignored, it freezes the content in state it was compiled.



Since I will run the app only locally I read my data this way:



typings.d.ts:



declare module "*.json" 
const value: any;
export default value;



game.service.ts:



import * as data_json from 'assets/data/game.json';

@Injectable()
export class GameService

constructor()
loadGames(): Observable<Game>
return Observable.of(JSON.parse(JSON.stringify(data_json)));




I don't use HTTPClient because I would run into CORS problems.
Reading the data works fine, but changes in the file game.json are ignored completely.



Is there a way to make this file "modifyable" so I can change it in the assets folder of the build?










share|improve this question














I want my angular app to work as a Standalone without a server.



In the assets folder are images, sound-files etc. and one game.json file which contains the business data for the application (On a server this data would be stored in a DB or would be called via Rest).



Everything works quite well. Using 'ng build' I can create the complete build which can be startet by opening the index.html in my OS - file manager without a http-Server.



The problem is that I can't change the content of game.json after the build, because it seems to be included in the build in some way. Changes are ignored, it freezes the content in state it was compiled.



Since I will run the app only locally I read my data this way:



typings.d.ts:



declare module "*.json" 
const value: any;
export default value;



game.service.ts:



import * as data_json from 'assets/data/game.json';

@Injectable()
export class GameService

constructor()
loadGames(): Observable<Game>
return Observable.of(JSON.parse(JSON.stringify(data_json)));




I don't use HTTPClient because I would run into CORS problems.
Reading the data works fine, but changes in the file game.json are ignored completely.



Is there a way to make this file "modifyable" so I can change it in the assets folder of the build?







angular assets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 9:40









Otis OttingtonOtis Ottington

919




919












  • Why you don't want to use HTTPClient to get data from assets folder? I don't think you will have any issue related to CORS if you it.

    – Trimantra Software Solution
    Mar 22 at 9:48











  • I tried to, but I ran into CORS issues.

    – Otis Ottington
    Mar 22 at 9:50











  • You can try my solution mentioned below.

    – Trimantra Software Solution
    Mar 22 at 9:59

















  • Why you don't want to use HTTPClient to get data from assets folder? I don't think you will have any issue related to CORS if you it.

    – Trimantra Software Solution
    Mar 22 at 9:48











  • I tried to, but I ran into CORS issues.

    – Otis Ottington
    Mar 22 at 9:50











  • You can try my solution mentioned below.

    – Trimantra Software Solution
    Mar 22 at 9:59
















Why you don't want to use HTTPClient to get data from assets folder? I don't think you will have any issue related to CORS if you it.

– Trimantra Software Solution
Mar 22 at 9:48





Why you don't want to use HTTPClient to get data from assets folder? I don't think you will have any issue related to CORS if you it.

– Trimantra Software Solution
Mar 22 at 9:48













I tried to, but I ran into CORS issues.

– Otis Ottington
Mar 22 at 9:50





I tried to, but I ran into CORS issues.

– Otis Ottington
Mar 22 at 9:50













You can try my solution mentioned below.

– Trimantra Software Solution
Mar 22 at 9:59





You can try my solution mentioned below.

– Trimantra Software Solution
Mar 22 at 9:59












1 Answer
1






active

oldest

votes


















1














You can get data from assets folder like shown below and you can also change it run time.



import Injectable from "@angular/core";
import HttpClient from "@angular/common/http";

@Injectable(
providedIn: "root"
)
export class GameService
private gameData: any;

constructor(private http: HttpClient)

loadGameData()
return this.http
.get("/assets/data/game.json")
.toPromise()
.then(data =>
this.gameData= data;
);


get gameData()
if (!this.gameData)
throw Error("Game file not loaded!");

return this.gameData;




app.module.ts changes:



import NgModule, APP_INITIALIZER from "@angular/core";
import GameService from "./services/GameService.service";

@NgModule(
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
[

provide: APP_INITIALIZER,
multi: true,
deps: [GameService],
useFactory: (gameService: GameService) =>
return () =>
return gameService.loadGameData();
;


]
],
bootstrap: [AppComponent]
)





share|improve this answer























  • I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

    – Otis Ottington
    Mar 25 at 8:28











  • provide GameService in app module providers above APP_INITIALIZER code , it will work.

    – Trimantra Software Solution
    Mar 25 at 10:43












  • I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

    – Otis Ottington
    Mar 25 at 11:52











  • Hi Trimantra Software Solution. Finally got it working. Thx!

    – Otis Ottington
    Mar 25 at 14:48











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296737%2fangular-changing-data-in-assets-folder-of-the-build%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can get data from assets folder like shown below and you can also change it run time.



import Injectable from "@angular/core";
import HttpClient from "@angular/common/http";

@Injectable(
providedIn: "root"
)
export class GameService
private gameData: any;

constructor(private http: HttpClient)

loadGameData()
return this.http
.get("/assets/data/game.json")
.toPromise()
.then(data =>
this.gameData= data;
);


get gameData()
if (!this.gameData)
throw Error("Game file not loaded!");

return this.gameData;




app.module.ts changes:



import NgModule, APP_INITIALIZER from "@angular/core";
import GameService from "./services/GameService.service";

@NgModule(
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
[

provide: APP_INITIALIZER,
multi: true,
deps: [GameService],
useFactory: (gameService: GameService) =>
return () =>
return gameService.loadGameData();
;


]
],
bootstrap: [AppComponent]
)





share|improve this answer























  • I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

    – Otis Ottington
    Mar 25 at 8:28











  • provide GameService in app module providers above APP_INITIALIZER code , it will work.

    – Trimantra Software Solution
    Mar 25 at 10:43












  • I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

    – Otis Ottington
    Mar 25 at 11:52











  • Hi Trimantra Software Solution. Finally got it working. Thx!

    – Otis Ottington
    Mar 25 at 14:48















1














You can get data from assets folder like shown below and you can also change it run time.



import Injectable from "@angular/core";
import HttpClient from "@angular/common/http";

@Injectable(
providedIn: "root"
)
export class GameService
private gameData: any;

constructor(private http: HttpClient)

loadGameData()
return this.http
.get("/assets/data/game.json")
.toPromise()
.then(data =>
this.gameData= data;
);


get gameData()
if (!this.gameData)
throw Error("Game file not loaded!");

return this.gameData;




app.module.ts changes:



import NgModule, APP_INITIALIZER from "@angular/core";
import GameService from "./services/GameService.service";

@NgModule(
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
[

provide: APP_INITIALIZER,
multi: true,
deps: [GameService],
useFactory: (gameService: GameService) =>
return () =>
return gameService.loadGameData();
;


]
],
bootstrap: [AppComponent]
)





share|improve this answer























  • I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

    – Otis Ottington
    Mar 25 at 8:28











  • provide GameService in app module providers above APP_INITIALIZER code , it will work.

    – Trimantra Software Solution
    Mar 25 at 10:43












  • I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

    – Otis Ottington
    Mar 25 at 11:52











  • Hi Trimantra Software Solution. Finally got it working. Thx!

    – Otis Ottington
    Mar 25 at 14:48













1












1








1







You can get data from assets folder like shown below and you can also change it run time.



import Injectable from "@angular/core";
import HttpClient from "@angular/common/http";

@Injectable(
providedIn: "root"
)
export class GameService
private gameData: any;

constructor(private http: HttpClient)

loadGameData()
return this.http
.get("/assets/data/game.json")
.toPromise()
.then(data =>
this.gameData= data;
);


get gameData()
if (!this.gameData)
throw Error("Game file not loaded!");

return this.gameData;




app.module.ts changes:



import NgModule, APP_INITIALIZER from "@angular/core";
import GameService from "./services/GameService.service";

@NgModule(
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
[

provide: APP_INITIALIZER,
multi: true,
deps: [GameService],
useFactory: (gameService: GameService) =>
return () =>
return gameService.loadGameData();
;


]
],
bootstrap: [AppComponent]
)





share|improve this answer













You can get data from assets folder like shown below and you can also change it run time.



import Injectable from "@angular/core";
import HttpClient from "@angular/common/http";

@Injectable(
providedIn: "root"
)
export class GameService
private gameData: any;

constructor(private http: HttpClient)

loadGameData()
return this.http
.get("/assets/data/game.json")
.toPromise()
.then(data =>
this.gameData= data;
);


get gameData()
if (!this.gameData)
throw Error("Game file not loaded!");

return this.gameData;




app.module.ts changes:



import NgModule, APP_INITIALIZER from "@angular/core";
import GameService from "./services/GameService.service";

@NgModule(
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
[

provide: APP_INITIALIZER,
multi: true,
deps: [GameService],
useFactory: (gameService: GameService) =>
return () =>
return gameService.loadGameData();
;


]
],
bootstrap: [AppComponent]
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 9:58









Trimantra Software SolutionTrimantra Software Solution

2,55811430




2,55811430












  • I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

    – Otis Ottington
    Mar 25 at 8:28











  • provide GameService in app module providers above APP_INITIALIZER code , it will work.

    – Trimantra Software Solution
    Mar 25 at 10:43












  • I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

    – Otis Ottington
    Mar 25 at 11:52











  • Hi Trimantra Software Solution. Finally got it working. Thx!

    – Otis Ottington
    Mar 25 at 14:48

















  • I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

    – Otis Ottington
    Mar 25 at 8:28











  • provide GameService in app module providers above APP_INITIALIZER code , it will work.

    – Trimantra Software Solution
    Mar 25 at 10:43












  • I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

    – Otis Ottington
    Mar 25 at 11:52











  • Hi Trimantra Software Solution. Finally got it working. Thx!

    – Otis Ottington
    Mar 25 at 14:48
















I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

– Otis Ottington
Mar 25 at 8:28





I use angular 5. so there is no 'providedIn' property in the annotation. I marked the service with Injectable and added the provider part in game.module.ts. I removed the providers in the comp. -> Exception: StaticInjectorError()[InjectionToken Application Initializer -> GameService]: NullInjectorError: No provider for GameService! If I add the service to the provider part in the component, it does not work either

– Otis Ottington
Mar 25 at 8:28













provide GameService in app module providers above APP_INITIALIZER code , it will work.

– Trimantra Software Solution
Mar 25 at 10:43






provide GameService in app module providers above APP_INITIALIZER code , it will work.

– Trimantra Software Solution
Mar 25 at 10:43














I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

– Otis Ottington
Mar 25 at 11:52





I did. GameService is as you suggested. app.module.ts now contains the APP_INITIALIZER - provider with deps and factory, etc. The comp. that uses the service is in submodule game.module.ts. Do I have to register the service in the comp. or the module as well? Or is the service accessable "from root". i.e. generally from everywhere?

– Otis Ottington
Mar 25 at 11:52













Hi Trimantra Software Solution. Finally got it working. Thx!

– Otis Ottington
Mar 25 at 14:48





Hi Trimantra Software Solution. Finally got it working. Thx!

– Otis Ottington
Mar 25 at 14:48



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296737%2fangular-changing-data-in-assets-folder-of-the-build%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해