简体   繁体   English

使用angular.js获取和更新json

[英]Get and update json using angular.js

I am very new to angular.js and am having some trouble with a seemingly simple task. 我对angular.js很新,并且在看似简单的任务时遇到了一些麻烦。

I need to get the json below from a json file on a website, then place the keys (english, spanish, etc.) inside label tags in my html file, then load their corresponding values (0, 1, 3, 2, 1) into html range inputs. 我需要从网站上的json文件中获取json,然后将键(英语,西班牙语等)放在我的html文件中的标签标签内,然后加载它们对应的值(0,1,3,2,1) )进入html范围输入。

The json file contains: json文件包含:

[{"english":0,"spanish":1,"german":3,"russian":2,"korean":1}]

The html produced after loading the json should look like: 加载json后生成的html应如下所示:

<form>
    <label>English</label>
    <input type="range" min="0" max="4" value="ENGLISH_VALUE_RETRIEVED_FROM_JSON_FILE" >
    <label>Spanish</label>
    <input type="range" min="0" max="4" value="SPANISH_VALUE_RETRIEVED_FROM_JSON_FILE" >
    <label>German</label>
    <input type="range" min="0" max="4" value="GERMAN_VALUE_RETRIEVED_FROM_JSON_FILE" >
    <label>Russian</label>
    <input type="range" min="0" max="4" value="RUSSIAN_VALUE_RETRIEVED_FROM_JSON_FILE" >
    <label>Korean</label>
    <input type="range" min="0" max="4" value="KOREAN_VALUE_RETRIEVED_FROM_JSON_FILE" >
    <input type="submit" text="Save">
</form>

Finally I want to hit Save on the form and have the values for the corresponding keys updated on the json file online. 最后,我想在表单上点击“保存”,并在线上更新json文件中相应键的值。

What are all the files necessary (using MVC) to build this example? 构建此示例所需的所有文件(使用MVC)是什么? If your answer involves any code, can you please explicitly say which file to add the code to? 如果您的答案涉及任何代码,请您明确说明要将代码添加到哪个文件?

Thanks in advance! 提前致谢!

Here's something to get you started. 这是让你入门的东西。 I changed your json to something that I believe is more appropriate, but you can change it back for your purposes if you wish. 我将你的json更改为我认为更合适的东西,但如果你愿意,你可以将它改回来。 If you do use your json, you'll have a problem with ng-repeat finding duplicate values and you'll need to use track by $index to fix it. 如果你确实使用了你的json,你会遇到ng-repeat查找重复值的问题,你需要使用track by $index来修复它。 See this post (click) . 看到这篇文章(点击)

Live demo here (click). 现场演示(点击)。

var app = angular.module('myApp', []);

/* $http ajax calls really belongs in a service, 
but I'll be using them inside the controller for this demo */ 

app.controller('myCtrl', function($scope, $http) {
  /*$http.get('path/to/json').then(function(data) {
    $scope.languages = data;
  });*/
  //inputting json directly for this example
  $scope.languages = [        
    {name:"English", value:0},
    {name:"Spanish", value:1},
    {name:"German", value:3},
    {name:"Russian", value:2},
    {name:"Korean", value:1}
  ];
  $scope.save = function() {
    /*$http.post('path/to/server/file/to/save/json', $scope.languages).then(function(data) {
      $scope.msg = 'Data saved';
    });*/
    $scope.msg = 'Data sent: '+ JSON.stringify($scope.languages);
  };
});

You'll want to read the information in this post (click) if you want to avoid wrapping your markup in an extra div. 如果你想避免将标记包装在一个额外的div中,你会想要阅读这篇文章中的信息(点击)

<form>
    <div ng-repeat="lang in languages">
      <label>{{lang.name}}</label>
      <input type="range" min="0" max="4" ng-model="lang.value" >
    </div>
    <button ng-click="save()">Save</button>
    <p>{{msg}}</p>
</form>

You can not use $http.post in angular to save. 你不能在角度中使用$ http.post来保存。 Client side code can not and should not save to the server. 客户端代码不能也不应该保存到服务器。 You can imagine how DANGEROUS this would be. 你可以想象这将是多么危险。

Instead... you can setup a NodeJS/Express (or whatever) to accept the JSON data in an $http.post. 相反......您可以设置NodeJS / Express(或其他)以接受$ http.post中的JSON数据。 Then you can write the data to a file using a server-side platform. 然后,您可以使用服务器端平台将数据写入文件。 However, I would recommend not saving data to a .json file on the server. 但是,我建议不要将数据保存到服务器上的.json文件中。 I make my .json files read-only and mainly used to populate static variables. 我将.json文件设为只读,主要用于填充静态变量。 You should look into storing these types of documents in a document store like CouchDB or MongoDB. 您应该考虑将这些类型的文档存储在CouchDB或MongoDB等文档存储中。

您无法修改服务器上的文件, $http.post无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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