ReferenceError:未与nodejs一起定义jurusan

时间:2018-10-28 06:29:43

标签: javascript mysql angularjs node.js

所以,我对此很陌生,尝试了angularJS和nodeJS大约2天,我陷入了这个错误。
我试图做一个动态的选择选项,我试图使用nodeJS从mysql数据库中回显一个表并将数据扔给有角度的。我已经尝试了很多方法来解决此问题,但是也许我仍然对角度问题还不太了解,所以我还没有解决问题。
我正在使用nodeJS ver。 1.0.0和angularJS版本。 1.7.5。

这是我的代码

controlAll.js

var http = require('http');
var qs = require('querystring');
var fs = require('fs');
var hbs = require('hbs');
var express = require('express');
var bodyParser = require('body-parser');
var route = express();
var mysql = require('mysql');

var konek = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "mahasiswa",
});
konek.connect(function(err){
    if(err) throw err;
    console.log("udah konek");
});

route.set('view engine', 'hbs');
route.use(bodyParser.json());
route.use(bodyParser.urlencoded({ extended: true }));

route.get('/mahasiswa/add', function(req, res){
    res.render("formm");
});
route.get('/jurusan', function(req,res){
    konek.query("SELECT * FROM `jurusan`",function(err,jurusan)
    {
        if (err) throw err;
        res.send(JSON.stringify({
            jurusan:jurusan
        }));
    });
});

formm.hbs

<!DOCTYPE html>
<html>
<head>
    <title>tambah mahasiswa</title>
    <link rel="stylesheet" href="/asset/css/bootstrap.min.css">
    <script src="/asset/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/ang/angular.js"></script>
</head>
<body ng-app="coba">
    <div class="container">
        <form method="POST" action="/mahasiswa/add">
            <div class="form-group">
                <label>Nama Mahasiswa</label>
                <input type="text" name="nama" placeholder="nama" class="form-control">
            </div>
            <div class="form-group">
                <label>Nomor Pokok Mahasiswa</label>
                <input type="text" name="npm" placeholder="npm" class="form-control">
            </div>
            <div ng-controller="drpdn">
                <div class="form-group">
                    <label>Jurusan</label>
                    <select name="id_jurusan" ng-model="jurusan" class="form-control">
                        <option disabled selected value>--Pilih Jurusan--</option>
                        <option ng-repeat="jur in jurusan" value="{{jur.id_jurusan}}">{{jur.namajurusan}}</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Program Studi</label>
                    <select name="id_prodi" class="form-control">
                        <option disabled selected value>--Pilih Program Studi--</option>
                    </select>
                </div>
            </div>
            <button type="submit" class="btn btn-primary">simpan</button>
        </form>
    </div>
</body>
</html>

<script type="text/javascript">
    var app = angular.module('coba',[]);
app.controller('drpdn', function($scope, $http){
       $http.get("/jurusan")  
       .then(function(response){
            $scope.jurusan = response.jurusan;
       });
  console.log($scope.jurusan);

});     

我的nodejs响应,我将数组放在另一条路由中 nodejs

console.log($scope.jurusan)

enter image description here

这是我的错误my error

另一个错误error 2

1 个答案:

答案 0 :(得分:0)

您的控制器(或提供服务)应从您的nodejs端点加载jurusan数据,然后使用它:

app.controller('drpdn', function($scope, $http){
    // Simple GET request example:
    $http({
        method: 'GET',
        url: '/jurusan' // USE YOUR NODEJS ENDPOINT TO LOAD DATA
    }).then(function(response) {
        // this callback will be called asynchronously
        // when the response is available
        $scope.jurusan = response.data.jurusan;
    }, function(response) {
      // called asynchronously if an error occurs
      // or server returns response with an error status.
    });
});