Angular无法从json中访问ng-model

Angular unable to access ng-model from json

本文关键字:访问 ng-model json Angular      更新时间:2023-09-26

我是angular和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)
    });
});

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

任何帮助都是感激的,谢谢提前

你混淆了ng-modelng-bind:

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

参见更新后的plunker

如果您需要遍历集合,请使用ng-repeat

你可以像这样在你的html中转储它:

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

它应该工作,它为我做了。你可能认为你看到了它,因为标签在<元素。你想要它做什么?>

在li标签中使用ng-repeat:

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