我有通過控制器查詢數據庫的JSON響應,我想在view.ejs文件中打印此JSON對象值。當數據庫調用時,控制器響應返回JSON對象時無法在view.ejs中打印JSON對象值
view.ejs
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.username}}</td>
<td>{{ x.password}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("")
.then(function (response) {$scope.names = response.users;});
});
</script>
這是我的模型 user.js的
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
username:{
type:'string',
required: true,
unique:true
},
password:{
type:'string',
required:true
},
}
};
這是我的控制器 UserController.js
module.exports = {
get:function(req,res){
User.find().exec(function (err, users){
if (err) {
return res.json(err);
}
return res.json(users);
});
}
};
我配置在以下路線文件 routes.js
module.exports.routes = {
'/': {
view: 'view'
},
'/user/get': 'UserController.get',
};
當我點擊URL「http://localhost:1337/user/get」時,我在json響應下面得到了這個,我想把這個值推入到view.ejs文件中。
我想知道,如果有人可以幫助我瞭解如何我可以得到這些values.Thank你的任何建議
嘗試在您的視圖中添加{{names}}您是否在獲取 – santhosh
中的用戶否我沒有獲取values.Please保留此建議 – user3534759
$ scope.names = response.users - >添加$ scope。$ apply($ scope。名稱)並檢查您的視圖中的{{名稱} – santhosh