在进行数据库调用时,如果控制器响应返回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.jsconfiguration路由
module.exports.routes = { '/': { view: 'view' }, '/user/get': 'UserController.get', };
当我点击URL“ http:// localhost:1337 / user / get ”时,我得到这下面的json响应,我想这个值被推入view.ejs文件。
[ { "username": "root", "password": "root", "id": 1 }, { "username": "home", "password": "home", "id": 2 }, { "username": "unisys", "password": "unisys", "id": 3 } ]
我想知道如果有人能帮助我了解我如何获得这些价值。感谢您的任何build议
从response
数据分配names
的问题。 即
使用
$ scope.names = response。 数据用户; OR $ scope.names = response。 数据 ;
代替
$ scope.names = response.users;
作为回应本身你得到的对象数组,所以如果你直接分配给$ scope.names,它将工作。
如果它帮助你,只要让我知道。
$ http.get(“localhost:1337 / user / get”)。then(function(response){$ scope.names = response; console.log(response);})
- 方式发送标题键与angular度得到/张贴expression4 rest api
- “EMALFORMED无法读取bower.json”“意外的令牌/”
- 使用节点缓冲区或fileStream与formDatafile upload
- Yeoman,Angular Generator和Karma Test不起作用
- Node + Express + Angular + Angular-route超出最大调用堆栈大小
- 如何以angular度显示图像(图像在服务器端上传文件夹和angularjs在不同的服务器上工作)?
- $ http Post上的Angularjs – 500(内部服务器错误)
- 无法将新模块添加到AngularJs Boilerplate
- 为什么我的AngularJS / Express / Socket.io应用程序不能提供socket.io.js文件?