【问题标题】:angular + node(express) + mysql角度+节点(快递)+ mysql
【发布时间】:2017-04-08 18:06:11
【问题描述】:

如何一起使用所有这些?例如,我有一个简单的数据库,其中包含一个表(ID、文本、完成)和一个将数据从 mySQL 导出到 JSON 文件的代码。 JSON 文件如下所示:

[{ "id":"1", "text":"learn angular", "done":true },
 { "id":"2", "text":"build an angular app", "done":false}]

还有 server.js 文件:

var fs = require('fs');
var serverUrl = 'localhost';
var port = 3000;
var path = require('path');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var mysql = require('mysql');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
app.use(bodyParser.json()); // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app); // pass our application into our routes
app.get('/', function(req, res){
res.sendfile('public/index.html');
});

var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : '',
    database : 'todos',
});

connection.connect();

app.get('/',function(request,response){
connection.query('SELECT * FROM todos', function(err, rows, fields, res)

{
        console.log('Connection result error '+err);
        console.log('no of records is '+rows.length);
        response.writeHead(200, { 'Content-Type': 'application/json'});
        response.end(JSON.stringify(rows));
        });

app.set('port', 3000);

http.listen(port, function(){
console.log('listening on: ' + app.get('port'));
});

exports = module.exports = app;

接下来我该怎么做?如何在 angularjs 应用程序中使用从 mysql 导出到 JSON 数据? index.html 文件:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<base href="/">

<title></title>

<!-- CSS -->

<!-- JS -->

<!-- ANGULAR CUSTOM -->
</head>

<body ng-app="App" ng-controller="Controller">
<div class="someClass">
<table>
<tr>
    <th>id</th>
    <th>text</th>
    <th>done</th>
</tr>
<tr ng-repeat="todo in todos">
    <td>{{todo.id}}</td>
    <td>{{todo.text}}</td>
    <td>{{todo.done}}</td>
</tr>
</table>
<div>
</body>
</html>

我理解正确的整个机制吗?我们在 mysql 中有一些数据库,将 mysql 数据转换为 json 对象,然后用角度读取这些对象?如果是这样,那么我们如何对数据库执行 crud 操作?提前感谢您提供任何有助于我理解它的建议和答案。

【问题讨论】:

标签: mysql angularjs json node.js express


【解决方案1】:

您可以使用 $http serviceice 从 API 获取值。像这样的:

$http({
    method: "POST",
    url: "youUrl",
    data: { "foo": "bar", "baz": "moe" },
    headers: {'Content-Type': 'application/json'} //optional
}).then(function(response){
   console.log(response) // success function
}, function(err){
   console.log(err) // error function
});

响应将包含您从后端发送的 API 数据。

【讨论】:

  • 仍然不明白如何提供可以与 angular 一起使用的数据。例如,我有以下代码: $scope.getTodos = function() { var httpRequest = $http({ method: 'POST', url: '../todos.json', data: what should be here? }) .success(function(data, status) { $scope.todos = data; }); };
  • @Kirill 请看答案
猜你喜欢
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2020-04-02
相关资源
最近更新 更多