referenceerror:jurusan没有用nodejs在angular中定义

7cwmlq89  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(355)

我是新来的尝试angularjs和nodejs,我陷入了这个错误。
我试着做一个动态选择选项,我试着使用nodejs从mysql数据库回显一个表,并将数据抛出到angular。我试了很多方法来解决这个问题,但也许我还是不太了解angular,所以我还没有解决这个问题。
我用的是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
        }));
    });
});

格式M.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);
});
</script>

我的nodejs回应,我把数组放在另一条路径上:

console.log($scope.jurusan)


我的错误是:

另一个错误:

snvhrwxg

snvhrwxg1#

您的控制器(或创建服务)应该加载 jurusan 来自nodejs端点的数据,然后使用它:

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.
    });
});

相关问题