Angular无法从json

时间:2015-09-26 14:07:47

标签: javascript json angularjs

我是角度和js的新手 请帮助我获取json密钥并绑定到ng-model

以下是我的代码

<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
  <li ng-model="table.macid"></li>
</ul>

我的js是

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $scope.table={}

  $http.get("test.json")
    .success(function(response) {

      $scope.names = response
      $scope.table.macid= Object.keys($scope.names.day.weekday.Monday.mac_id)
      console.log($scope.table)
    });
});

这是我的plunker链接http://plnkr.co/edit/6CoOAp0X8FZw1JODXSHT?p=preview

感谢任何帮助,提前致谢

3 个答案:

答案 0 :(得分:4)

您将ng-modelng-bind混淆:

<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
  <li ng-bind="table.macid"></li>
</ul>

参见 updated plunker

如果您需要迭代集合,请使用ng-repeat。请参阅zszep回答。

答案 1 :(得分:1)

您可以将其转储到您的html上,如下所示:

      <li ng-model="table.macid"></li>{{table.macid}}

它应该有用,它对我有用。您可能认为您看到了它,因为标签位于&lt; li>元件。你有什么期望呢?

答案 2 :(得分:1)

在li标记中使用ng-repeat,如plunker

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

<body>

  <div ng-app="myApp" ng-controller="customersCtrl">
    <ul>
      <li ng-repeat="macid in table.macid">{{macid}}</li>
    </ul>
  </div>


  <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
      $scope.table={}

      $http.get("test.json")
        .success(function(response) {

          $scope.names = response
          $scope.table.macid= Object.keys($scope.names.day.weekday.Monday.mac_id)

        });console.log($scope.table)
    });
  </script>

</body>

</html>