公共控制器和服务器控制器在 MEAN.io 中如何相互关联

How are public and server controllers related to each other in MEAN.io

本文关键字:控制器 何相互 io 关联 服务器 MEAN      更新时间:2023-09-26

从 MEAN.io 开始,他们提供了一个示例"文章"模型,该模型基本上类似于带有标题和正文的博客文章。

该示例附带一个index.html文件,当您导航到该文件时,该文件将显示文章列表。在此文件中,它调用公共控制器中定义的 find 方法,如下所示

$scope.find = function() {      
  Articles.query(function(articles) {
     $scope.articles = articles;
  });      
};

我看到一个定义以下方法的服务器控制器

/**
 * List of Articles
 */
exports.all = function(req, res) {
  Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
    if (err) {
      return res.json(500, {
        error: 'Cannot list the articles'
      });
    }
    res.json(articles);
  });
};

当我向服务器控制器中的 find 方法添加约束时,我可以有效地为查询定义where筛选器,这将反映在视图中。

这两个控制器之间是否存在由框架隐式处理的连接?我找不到有关所有这些如何关联的任何信息。

我直言。如果有过滤连接,代码必须像这样

/**
 * List of Articles
 *  use GET /api/v1/articles?published=true to filter
 */
exports.all = function(req, res) {
  Article
        .find(req.query) //this is filtering!
        .sort('-created')
        .populate('user', 'name username')
        .exec(function(err, articles) {
    if (err) {
      return res.json(500, {
        error: 'Cannot list the articles'
      });
    }
    res.json(articles);
  });
};