MEAN.JS新控制器function和路由

我正在学习MEAN.JS,并且在路由方面有一些问题,只是希望在名为getSubArticles的文章项目中添加一个新的函数,该函数调用服务器并执行一些操作。

//articles.server.routes.js app.route('/articles/:articleId/subarticles') .get(articles.read); //articles.server.controller.js exports.read = function(req, res) { res.jsonp("My Sub articles");//just using for testing }; //articles.client.controller.js $scope.getSubArticles = function() { // What goes here as everything I've tried has failed, but the route could be constructed incorrectly }; //articles.client.view.html <section id="subarticle-listing" data-ng-controller="ArticlesController" data-ng-init="getSubArticles()"> 

任何想法,我要去错了? 有一个示例MEAN.JS项目与非默认路线/function,我可以看看…

干杯,阿德里安

有一些东西跳出来,首先你应该确保你的路由在被请求时被击中,你可以在你的读取function中添加一个console.log。 在你的app.js(或server.js,无论哪个文件是用来启动你的应用程序),你需要有:

  //server.js file: var router = express.Router(); var routes = require('./articles.server.routes.js'); //path to your routes file router.use('/',routes); 

在你的articles.server.routes.js文件中,包含这些要求在顶部,你的路线不能有一个variables,然后一个固定的名字,基本上什么后variables将被砍掉,所以新的路线应该是:

 // articles.server.routes.js var express = require('express'), app = express().Router(); app.route('/articles/subarticles/:articleId') .get(articles.read); 

在您的articles.server.controller.js中:

  exports.read = function(req, res) { var articleid = req.params.bid console.log('read function called' + articleid) res.jsonp("My Sub articles");//just using for testing }; 

现在在你的Angular控制器中:

 $scope.getSubArticles = function() { $http.get('/articles/subarticles/' + $scope.articleId) .success(function(data){ //do something with your return data } .error(function(err){ //error handler } } 

希望有帮助,如果没有,让我知道,我可以build议更多的事情。