Get and update json using angular.js

前端 未结 3 1448
星月不相逢
星月不相逢 2020-12-15 01:54

I am very new to angular.js and am having some trouble with a seemingly simple task.

I need to get the json below from a json file on a website, then place the keys

3条回答
  •  死守一世寂寞
    2020-12-15 02:31

    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. 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. 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.

    {{msg}}

提交回复
热议问题