我对MEAN堆栈很新,并且正在尝试将新集合合并到数据库中。我将我的代码基于此示例http://jasonwatmore.com/post/2015/12/09/MEAN-Stack-User-Registration-and-Login-Example.aspx并且已经使用它的代码来登录和注册用户。
我基本上尝试做同样的事情,但是使用课程而不是用户,因此尝试复制影响课程集合的用户集合的所有文件和控制器。所有功能都适用于涉及用户的所有内容,但是当我尝试为课程执行相同操作时出现以下错误:未知提供者:CourseServiceProvider< - CourseService< - Admin.IndexController
我已经包含了我想象的所有代码,但如果需要其他任何内容,请告诉我。
我的目录如下所示
app
-account
--index.controller.js
--index.html
-admin
--index.controller.js
--index.html
-app-services
--course.service.js
--flash.service.js
--user.service.js
-home
--index.controller.js
--index.html
-app.js
-index.html
controllers
-api
--course.controller.js
--user.controller.js
-app.controller.js
-login.controller.js
-register.controller.js
services
-course.service.js
-user.service.js
views
-partials
--header
--footer
-login.ejs
-register.ejs
config.json
package.json
server.js
应用/管理/ index.controller.js
(function () {
'use strict';
angular
.module('app')
.controller('Admin.IndexController', Controller);
function Controller($window, CourseService, Flashservice) {
var vm = this;
vm.saveCourse = saveCourse;
vm.deleteCourse = deleteCourse;
function saveCourse(){
//Nothing here yet
}
function deleteCourse(){
//Nothing here yet
}
}
})();
应用/应用服务/ course.service.js
(function () {
'use strict';
angular
.module('app')
.factory('CourseService', Service);
function Service($http, $q){
var service = {};
service.GetCurrent = GetCurrent;
service.GetAll = GetAll;
service.GetById = GetById;
service.Create = Create;
service.Update = Update;
service.Delete = Delete;
return service
//Functions for all service.XX above, currently empty will
look something like this(copied from app/app-services/user.service.js):
// will user server.js who should call controllers/api/courses/method.
function GetCurrent() {
return $http.get('/api/users/current').then(handleSuccess, handleError);
}
// private functions
function handleSuccess(res) {
return res.data;
}
function handleError(res) {
return $q.reject(res.data);
}
}
})();
控制器/ course.controller
var config = require('config.json');
var express = require('express');
var router = express.Router();
var CourseService = require('services/course.service');
router.put('/:_id', updateCourse);
router.delete('/:_id', deleteCourse);
module.exports = router;
function updateCourse(req,res) {
//Put code here, look at users.controller för inspo
}
function deleteCourse(req,res){
//Put code here, look at users.controller för inspo
}
服务/ course.service.js
var config = require('config.json');
var _ = require('lodash');
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs');
var Q = require('q');
var mongo = require('mongoskin');
var connectionString = "mongodb://localhost:27017/mean-stack-registration-login-example";
if(process.env.OPENSHIFT_MONGODB_DB_URL){
connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + "studycontrol";
}
var db = mongo.db(connectionString, { native_parser: true });
db.bind('courses');
var service = {};
service.create = create;
service.update = update;
service.delete = _delete;
module.exports = service;
function create(courseParam){
//put code here
createCourse();
}
function createCourse(){
//insert course into db
}
function update(_id, courseParam){
//put code here
updareCourse();
function updateCourse(){
//updaet course in database
}
function _delete(_id){
//delete course from db
}
server.js
require('rootpath')();
var express = require('express');
var app = express();
var session = require('express-session');
var expressJwt = require('express-jwt');
var config = require('config.json');
var bodyParser = require('body-parser');
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({ secret: config.secret, resave: false, saveUninitialized: true }));
// use JWT auth to secure the api
app.use('/api', expressJwt({ secret: config.secret }).unless({ path: ['/api/users/authenticate', '/api/users/register'] }));
// routes
app.use('/login', require('./controllers/login.controller'));
app.use('/register', require('./controllers/register.controller'));
app.use('/app', require('./controllers/app.controller'));
app.use('/api/users', require('./controllers/api/users.controller'));
// make '/app' default route
app.get('/', function (req, res) {
return res.redirect('/app');
});
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
// start server
var server = app.listen(port, ipaddress);
编辑 - 包含app.js,位于
下方(function () {
'use strict';
angular
.module('app', ['ui.router'])
.config(config)
.run(run);
function config($stateProvider, $urlRouterProvider) {
// default route
$urlRouterProvider.otherwise("/");
$stateProvider
.state('home', {
url: '/',
templateUrl: 'home/index.html',
controller: 'Home.IndexController',
controllerAs: 'vm',
data: { activeTab: 'home' }
})
.state('account', {
url: '/account',
templateUrl: 'account/index.html',
controller: 'Account.IndexController',
controllerAs: 'vm',
data: { activeTab: 'account' }
})
.state('admin',{
url: '/admin',
templateUrl: 'admin/index.html',
controller: 'Admin.IndexController',
controllerAs: 'vm',
data:{activeTab: 'admin'}
});
}
function run($http, $rootScope, $window) {
// add JWT token as default auth header
$http.defaults.headers.common['Authorization'] = 'Bearer ' + $window.jwtToken;
// update active tab on state change
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
$rootScope.activeTab = toState.data.activeTab;
});
}
// manually bootstrap angular after the JWT token is retrieved from the server
$(function () {
// get JWT token from server
$.get('/app/token', function (token) {
window.jwtToken = token;
angular.bootstrap(document, ['app']);
});
});
})();