【问题标题】:AngularJS Code not working with NodeJSAngularJS代码不适用于NodeJS
【发布时间】:2015-02-13 00:04:53
【问题描述】:

我是 MEAN Stack 的新手。我用一些 angularJS 在 html 中尝试了一些非常基本的东西,当我在浏览器中打开它时它正在工作。但不幸的是,当我尝试用我的 nodeJS 服务器渲染它时,代码不再工作了。 index.html 已显示,但角度部分不再起作用。我的输出只是 {{article}}。你有什么建议吗?

index.html

<html ng-app="ibrahimsBlog">

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Ibrahims Blog</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body>

  <div ng-controller="articleController">
    <div ng-repeat="article in articles">
      {{article}}
    </div>
  </div>  

  </body>

</html>

app.js

var app = angular.module('ibrahimsBlog', [])
app.controller('articleController', [
'$scope',
function($scope) {
  $scope.articles = [
    'foo',
    'bar',
    'baz'
  ];
}]);

还有我非常基本的节点服务器:

var express = require('express'), app = express();

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

app.listen(3000, function() {
  console.log('Server listening on port 3000');
});

【问题讨论】:

    标签: angularjs node.js mean-stack


    【解决方案1】:

    SERVER.JS 更改为:

    var express = require('express');
    var app = express();
    
    /* ==========================================================
    serve the static index.html from the public folder
    ============================================================ */
    app.use(express.static(__dirname + '/public'));
    
    app.listen(3000, function() {
      console.log('Server listening on port 3000');
    });
    

    【讨论】:

    • 解释一下:问题是你的 JS 文件没有被提供——你只是告诉 express 提供 '/'
    • 我刚起床试了一下,效果很好,非常棒 :) 但为什么现在还能用呢?
    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多