我有一个没有正确数据的JSON对象。所以我想用从查询表中获得的值替换获得的值,但是我不确定如何替换与查询表相对应的数据的值。

lookupTable = {
  "pizza": function() {
    console.log("food");
  },
  "house": function() {
    console.log("building");
  },
  "air": function() {
    console.log("nothing");
  }
};
$scope.value =lookupTable["pizza"]()


我的html文件有

<tr ng-repeat="x in names">
    <td>{{ x.lookupTable["pizza"]() }}</td>




我的代码在http://plnkr.co/edit/w4lOFVRo9vSi8vqfbpXV?p=preview

任何帮助表示赞赏!

最佳答案

您的代码非常混乱。我试图编辑您的plunker,也许更容易理解。

<!DOCTYPE html>
<html>
<scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="customersCtrl">

    <table>
      <tr ng-repeat="x in names">
        <td>{{ lookupTable[x.Name] }}</td>
      </tr>
    </table>

  </div>

  <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
      $http.get("data.json")
        .then(function(response) {
          $scope.names = response.data.records;
      });


    lookupTable = {
      "pizza": "food",
      "house": "building",
      "air": "nothing"
    };
    $scope.lookupTable = lookupTable;
    $scope.value = lookupTable["pizza"]
    console.log(lookupTable["house"])
    });
  </script>

关于javascript - Angular ng-repeat需要更改从json获得的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34197752/

10-11 14:21