我正在使用angularjs 1.6.4和nedb。
当我尝试使数据库中的每个用户中的每个用户与每个用户一起返回JSON文件时。因此,我尝试使用AngularJS显示它们,但是当我尝试使用ng-repeat="user in users"
显示它们时,我只能使用{{ user[0].name }}
显示它们,但它仅显示一个用户而不是每个用户,我希望它可以使用{{ user.name }}
来工作。
编辑:这就是我的JSON文件看起来像的方式
[
{ _id: 1,
name: 'Antoine',
createdAt: 2017-04-18T06:42:18.473Z,
updatedAt: 2017-04-18T06:42:18.473Z
},
{ _id: 2,
name: 'Louise',
createdAt: 2017-04-18T06:42:18.478Z,
updatedAt: 2017-04-18T06:42:18.478Z
}
]
编辑2:尝试@pansulbhatt的解决方案Pansul Bhatt的解决方案
似乎您想在整个JSON上迭代,所以您没有考虑它是列表,为什么不尝试像对象一样迭代它,类似于:
<tr ng-repeat="(key, value) in data">
<td> {{key}} </td> <td> {{ value }} </td>
</tr>
现在,您将拥有与定义给您的那些键关联的所有键和所有值。如果您可以发送JSON,我可以提供更多帮助,但我认为这会有所帮助。
好吧,首先,我要感谢您对我的问题的答案!但是我也想道歉,因为这是我的控制器的错误。我的代码是:
$scope.getAllUsers = function() {
$http.get("users").then(function(response) {
$scope.users = response;
});
}
我通过简单的修改解决了它:
$scope.getAllUsers = function() {
$http.get("users").then(function(response) {
$scope.users = response.data;
});
}
$scope.user=[
{ _id: 1,
name: 'Antoine',
createdAt: 2017-04-18T06:42:18.473Z,
updatedAt: 2017-04-18T06:42:18.473Z
},
{ _id: 2,
name: 'Louise',
createdAt: 2017-04-18T06:42:18.478Z,
updatedAt: 2017-04-18T06:42:18.478Z
}];
您的JSON无效,因为您的日期应该是字符串。运行以下片段(我还使用了日期格式(。
您可以使用ng-repeat
显示如下:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.users = [{
_id: 1,
name: 'Antoine',
createdAt: '2017-04-18T06:42:18.473Z',
updatedAt: '2017-04-18T06:42:18.473Z'
}, {
_id: 2,
name: 'Louise',
createdAt: '2017-04-18T06:42:18.478Z',
updatedAt: '2017-04-18T06:42:18.478Z'
}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="user in users">
{{user.name}} {{user.createdAt | date: 'dd/MM/yyyy'}}
</div>
</div>