0

我通过在mean.io上使用 MEAN 堆栈开始使用 node/express/Angular 。

我不明白 Angular 控制器如何调用 express 控制器来获取数据。

我所拥有的是 public/js/controllers/index.js:

angular.module('mean.system').controller('IndexController', ['$scope', 'Global', 'Tabs', 
    function ($scope, Global, Tabs) {
        $scope.global = Global;
        Tabs.query(function(tabs) {
            $scope.tabs = tabs;
        });
}]);

但我很困惑“标签”到底是什么。我知道不知何故,神奇地,最终调用了这个方法——我认为这是 Express 控制器?应用程序/控制器/tabs.js:

exports.all = function(req, res) {
    Tab.find().sort('artist').select("-content").populate('user').exec(function(err, tabs) {
    if (err) {
        res.render('error', {
            status: 500
        });
    } else {
        res.jsonp(tabs);
    }
});
};

但我不明白它是如何被调用的。我想要做的是在 app/controllers/tabs.js 中调用不同的方法 - 即:

exports.newest = function(req, res) {
    Tab.find().sort('-created').limit(10).select("-content").exec(function(err, tabs) {
    ...

但我不明白如何将 AngularJS 控制器与 express 控制器“连接”起来。

即我必须做什么才能在我的控制器中做这样的事情:

angular.module('mean.system').controller('IndexController', ['$scope', 'Global', 'Tabs', 
    function ($scope, Global, Tabs) {
        $scope.global = Global;
        Tabs.newest(function(tabs) { // this won't work
            $scope.tabs = tabs;
        });
}]);
4

1 回答 1

0

在 MEAN 中,Articles 服务是一个 Angular 服务,它返回一个 $resource 对象,您通常可以在 public/js/services 文件夹中找到它。

$resource 是 angularjs 附带的 $http AJAX 服务的包装器,如果您的 REST 服务以特定方式构建,它使您能够连接到 RESTful 端点。

与 node.js 控制器的连接使用 config 文件夹中的 routes.js 对象发生,该对象将路由绑定到特定模块方法。

进一步阅读:

http://docs.angularjs.org/api/ngResource.$resource

http://expressjs.com/api.html#app.VERB

于 2013-11-22T01:15:34.037 回答