简体   繁体   中英

Subscribe to a json file in angular

I am reading a variable from a JSON file, which is getting updated by a .NET application. In my angular component, I am taking input from this JSON file. Can we subscribe to any changes in this input JSON file?

The reason why I am reading from the JSON file because the workflow is like this. The .NET application is providing me with input, and on another server, an Angular web application is running which takes input. Through the route, I can't pass a complex object like this.

inputIDs = [
    {
      entityType: "A",
      guids: [
        "6EACCBAE83E",
        "F3354548399",
      ],
    },
    {
      entityType: "B",
      guids: [
        "4CB2FF7EC199",
        "DC60A9F4weCA",
      ],
    },
  ];

And that's why I decided to use a json file in between. Is this approach also correct?

If you want updates from this json file you will need to use "polling". You can read more about how to do this with rxjs here: Polling in rxjs .

This basicly will request the complete json file every X seconds/minutes. In Angular it will look like a normal GET http call to the correct file path/url where the file is located.

If you want to get the updates to the file instantaneously you will have to look into push messaging. Basicly sending messages from the server to the client. This is very easy to do in Angular but will require some backend work in setting this up.

And that's why I decided to use a json file in between. Is this approach also correct?

I would never recommend it, this is not the way modern webapps communicate with servers. You should define an endpoint in your backend which returns a json response to the frontend. This is because saving a file on a filesystem is quite error prone. You have the possibility for all kinds of locking and corruption to occure. This ofcourse depends in what kind of environment you are working and how much choice you have in modifying excisting code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM