javascript - Angular ngrepeat 不显示对象数组中的任何数据

标签 javascript angularjs

我想知道我在 Angular 上做错了什么,因为 ngrepeat 没有显示数据。

我这里有一个 fiddle http://jsfiddle.net/e0e7dee5/

<div ng-controller="MyCtrl">
    <div class="container-fluid">
        <div ng-repeat="u in utilities.all">
            {{u.name}}
        </div>
    </div>
</div>

上面应该是对的吧?

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

function MyCtrl($scope) {
    //var utilities = this;

    var utilities = {};

    utilities.all = [{ .... }];   // this is my data  it is in the fiddle
    console.log(utilities.all);  // this displays array of objects

}

最佳答案

您应该将 varutilities = {}; 更改为 $scope.utilities = {};

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

function MyCtrl($scope) {
	//var utilities = this;

  $scope.utilities = {};

  $scope.utilities.all = 
    [{
      "programId": 1062,
      "name": "Atlantic City Electric",
      "utilityTypeName": "Electric",
      "programName": "Test Program 24",
      "rate": 0.0775,
      "term": 12,
      "serviceReference": false,
      "accountNumberTypeName": "Account Number",
      "accountNumberLength": 10,
      "msf": 4.95,
      "etf": 100,
      "unitOfMeasureName": "KwH",
      "meterNumberLength": null,
      "zip": "85281",
      "$$hashKey": "object:325"
    }, {
      "programId": 1063,
      "name": "Atlantic City Electric",
      "utilityTypeName": "Electric",
      "programName": "Test Program 12",
      "rate": 0.0875,
      "term": 24,
      "serviceReference": false,
      "accountNumberTypeName": "Account Number",
      "accountNumberLength": 10,
      "msf": 5.95,
      "etf": 150,
      "unitOfMeasureName": "KwH",
      "meterNumberLength": null,
      "zip": "85281",
      "$$hashKey": "object:326"
    }, {
      "programId": 1064,
      "name": "Atlantic City Electric",
      "utilityTypeName": "Gas",
      "programName": "Test Gas Program 12",
      "rate": 0.555,
      "term": 12,
      "serviceReference": false,
      "accountNumberTypeName": "Account Number",
      "accountNumberLength": 10,
      "msf": 1,
      "etf": 10,
      "unitOfMeasureName": "Therm",
      "meterNumberLength": 5,
      "zip": "85281",
      "$$hashKey": "object:333"
    }];
 

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
 
 <div class="container-fluid" ng-repeat="u in utilities.all">
   <div>
   {{u.name}}
   </div>
 </div>
 
</div>

关于javascript - Angular ngrepeat 不显示对象数组中的任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42759706/

相关文章:

javascript - AngularJs 对所有页面的所有记录进行排序

node.js - Yeoman 构建的 Angular 应用程序未将 Bootstrap 字体部署到 Heroku

javascript - 使用动态内容附加 localStorage

javascript - 使用 "this"在表之间移动数据

javascript - 在 div 中翻转动画不隐藏 Canvas 。预期行为是隐藏 Canvas

javascript - 如何在angularjs中将数据从列表框添加到列表框

javascript - 当我没有任何记录时,默认选择所有复选框

javascript - 在AngularJS中,当制作指令时,有没有办法使用链接函数来操作模板的DOM?

javascript - 带挖空绑定(bind)的完美滚动条

javascript - 尝试访问 json 响应时出现“未定义”