我試圖在AngularJS中顯示對象的集合時出現問題。AngularJS不顯示ng-repeat,只是顯示一個空列表
ng-repeat僅顯示控制檯中的項目,但數據爲空。我正確地獲取了JSON數據,並獲得了我的api成功的範圍項目。 在我看來,它只是在底部顯示一個灰色條,數據打印在底部,而不是在ng-repeat上。
<div ng-controller="userController">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Users</h2>
</div>
<div class="panel-body">
<div class="user">
<div class="form-group">
<input ng-model="user.name" type="text" placeholder="User Name" class="form-control">
</div>
<div class="form-group">
<input ng-model="user.email" type="email" placeholder="User Email " class="form-control">
</div>
<div class="form-group">
<input ng-model="user.password" type="password" placeholder="Set Your Password" class="form-control" >
</div>
<div class="form-group">
<select ng-model="user.admin" name="" id="" class="form-control">
<option value="">Admin</option>
</select>
</div>
<div class="form-group">
<button ng-click="createUser(user)" class="btn btn-success">Create User</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3>All Users</h3>
</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>Email</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
{{users}}
請參閱 this image和this one。
(function() {
'use strict';
angular
.module('anicasioApp')
.controller('userController', userController);
function userController($scope, $http){
$scope.createUser = createUser;
$scope.users = [];
function init(){
getAllUsers();
}
init();
function getAllUsers() {
$http.get('/api/userpost/')
.then(function(users) {
$scope.users = users;
console.log($scope.users);
})
.catch(function(err) {
err.sendStatus(400);
})
}
function createUser(users) {
console.log(users);
$http.post('/api/userpost/', users)
.then(getAllUsers);
console.log(users);
}
}
})();
// Users Model
function getAllUsers(req, res) {
UserModel
.find()
.then(function(users) {
res.json(users);
}),
function(err){
res.sendStatus(400);
}
}
請問您能發佈您的html代碼嗎? – lealceldeiro
我張貼在上面Asiel Leal –