1

First of all, let me tell you that I'm a novice in the world of javascript and node.js. I have been searching for help in trying to do what i want but haven't found yet.

I am using the MEAN stack(http://mean.io/) and I am trying to implement a search feature in the included articles model. The search would look for articles with a specific tag and would be implemented in the index page. Follow me and see if you can find what I am missing please.

In the backend:

app/models/

/**
 * Article Schema
 */
var ArticleSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    title: {
        type: String,
        default: '',
        trim: true
    },
    content: {
        type: String,
        default: '',
        trim: true
    },
    tag: {
        type: String,
        default: '',
        trim: true
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

app/controllers/

exports.searcharticle = function(req, res) {
    Article.find({'tag': req.params.tag}).sort('-created').populate('user', 'name username').exec(function(err, articles) {
        if (err) {
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(articles);
        }
    });
};

Added the route for the search in app/routes/articles.js

app.get('/articles/search/:tag', articles.searcharticle);

In the frontend:

Created the view for the search wich will display the search results - public/views/articles/search.html

<section data-ng-controller="ArticlesController" data-ng-init="searchart()">
<ul class="articles unstyled">
<li data-ng-repeat="article in articles">
<span>{{article.created | date:'medium'}}</span> /
<span>{{article.user.name}}</span>
<h2><a data-ng-href="#!/articles/{{article._id}}">{{article.name}}</a></h2>
<div>{{article.tag}}</div>
</li>
</ul>
<h1 data-ng-hide="!articles || articles.length">Your search hasn't returned any results. <br> Why don't you <a href="/#!/articles/create">Create One</a>?</h1>
</section>

The view for the index.html, where the searchbox will be implemented

<section data-ng-controller="ArticlesController">
      <form role="form" data-ng-submit="searchart()">
        <div>
          <div>
            <input type="text" id="tag" ng-model="selected" class="form-control" placeholder="Tag">
          </div>
          <div>
            <button type="submit" class="btn btn-default">Submit</button>
          </div>
        </div> 
      </form>

Added the route to the config.js

when('/articles/search/:tag', {
            templateUrl: 'views/articles/search.html'
        }).

And added the search function to the articles controller

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

Right now, with this code implemented, when I click in the submit button in the index page, nothing happens.

Can you find what am I missing?

Thanks in advance!

4

1 回答 1

0

为了在您的客户端文章服务中使用 URL,您应该在文章服务中定义 URL 参数:packages/articles/public/services/article.js,就像已经在其中定义的 articleId 参数一样:

angular.module('mean.articles').factory('Articles', ['$resource',
  function($resource) {
    return $resource('articles/:articleId', {
      articleId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

然后你需要在你的角度控制器搜索方法中传递它,就像通过 id 获取一个的函数一样,像这样:

$scope.findOne = function() {
      Articles.get({
        articleId: $stateParams.articleId
      }, function(article) {
        $scope.article = article;
      });
    };

我个人不知道如何在 $resource 对象中添加现有参数(articleId)之外的另一个参数,您可能必须使用新参数(:tag)创建另一个 $resource 服务并在您的搜索方法中使用它在你的角度控制器中。

另一种对我来说听起来更简单灵活的方法是在查询方法中传递搜索参数,如下所示:

 $scope.searchart = function() {
    Articles.query({tag:$scope.selectedTag}, function(articles) {
        $scope.articles = articles;
    });
};

然后在服务器端控制器上,读取您的查询参数,如下所示:

exports.searcharticle = function(req, res) {
    Article.find(req.query).sort('-created').populate('user', 'name username').exec(function(err, articles) {
        if (err) {
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(articles);
        }
    });
};
于 2014-11-02T09:01:20.930 回答