【问题标题】:Running node.js app and getting angular is not defined message运行 node.js 应用程序并获取角度未定义消息
【发布时间】:2017-08-25 18:55:02
【问题描述】:

我正在尝试在 node.js 中运行一个应用程序,而我以前从未使用过 node,当我在终端中输入 node app.js 时,我收到以下错误消息:

ReferenceError: angular is not defined
at Object.<anonymous> (/Users/michael/Desktop/myapp/app/app.js:20:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3

我安装了角度。这是错误中提到的 app.js 文件的代码部分:

var app = angular
.module('gbook', ['ui.router', 'uiSwitch', 'LocalStorageModule', 'vsGoogleAutocomplete', 'ngCookies', 'gbook.config', 'gbook.intlTelInput', 'ui.timepicker', 'ngAnimate', 'ngSanitize', 'mgcrea.ngStrap', 'froala', 'ui.bootstrap'])
.config(config)
.run(run);

我在 angular 文件夹下的 index.js 中也有这段代码:

require('./angular');
module.exports = angular;

是我在安装 Angular 时做错了什么,还是其他原因?

【问题讨论】:

  • 如果您需要更多信息,请告诉我
  • Angular 是客户端框架,Node 是服务器端。为什么要在节点中运行角度? (可以在支持 Node.js 的情况下运行 Angular 应用程序,但您所做的是不同的,而且似乎是错误的)
  • 请检查您的库是否保存在公共文件夹中,以便它们在浏览器中正确加载

标签: angularjs node.js npm


【解决方案1】:

我认为您不能在服务器端使用 Angular。 Angular 用于客户端脚本。

【讨论】:

    【解决方案2】:

    您不能在服务器端使用 angular,但是您可以使用像 ejs 或 jam 这样的渲染引擎和 express 并在 .ejs 文件中使用 angular。这样,您还可以访问后端使用的变量。我建议谷歌搜索 expressjs 和 ejs 并对其进行一些研究。

    还有一件事,确保您的 AngularJS 通过 cdn 或 JavaScript 文件正确连接到前端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 2019-06-03
      相关资源
      最近更新 更多