要求未以角度定义 [英] require is not defined in angular
问题描述
我正在使用 angularjs,我在其中创建了一个 js 文件(loginCtrl.js)并且我包含了一个控制器.我已经在另一个文件(server.js)中定义了我的数据库连接和架构,我想将该文件包含在我的 js 中.
I am using angularjs where i have created a js file(loginCtrl.js) and i have inclused a controller. I have defined my db connection and schema in another file(server.js) and i want to include that file in my js.
我的 loginCtrl.js 看起来像:
My loginCtrl.js looks like:
test.controller('loginCtrl', function($scope, loginService){
$scope.login = function(user)
{
console.log('inside function 1');
var user = require('./server.js')
console.log('inside function');
loginService.login(user);
}
});
我的 server.js 文件看起来像:
and my server.js files looks like:
var express = require('express');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/userregistration');
var userSchema = {
username: String,
firstname:String,
lastname: String,
email: String,
password: String
}
var User = mongoose.model('User', userSchema, 'user');
当我运行代码时,它给出了如下错误:
When i run the code, it gives the error like:
ReferenceError: require is not defined
它正在打印上面定义的 console.log.
It is printing the console.log defined above.
推荐答案
require()
是一个 NodeJS 函数,你的 angular 控制器将在浏览器中执行,浏览器没有内置的——在功能上.如果您想在客户端复制该行为,您应该查看 RequireJS
require()
is a NodeJS function, your angular controller will be executed in the browser, which doesn't have that built-in function. If you want to replicate that behavior client-side, you should look at RequireJS
这篇关于要求未以角度定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!