Angular param查询不使用express API

时间:2017-08-31 14:07:27

标签: angularjs node.js express

大家下午好!当我查询localhost / post-number-2时,我得到了未定义。但是,当我访问api时,我可以检索正确的JSON对象 - 所以当我搜索localhost / api / articles / post-number-2时,API将完成它的工作并打印回来的信息。我不确定为什么它不使用角度路线。这是slug的路由。

.config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true); 
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl',
        controllerAs: 'main'
      })
      .when('/:slug', {
        templateUrl: 'views/article.html',
        controller: 'MainCtrl',
        controllerAs: 'main'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl',
        controllerAs: 'about'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

这是我的控制器。我试图使它成为正常显示缩略图,如果有一个参数请求显示整篇文章查询。

    angular.module('clientApp')  
      .controller('MainCtrl', ['$http','$routeParams', function ($http, $routeParams) {

        var scope = this;


        if($routeParams.slug){
          var slug = $routeParams.slug;


          var req = $http.get('/api/articles/' + slug);

          req.then(function (res) {
            scope.article_slug = res.data.article;
            console.log(res.data.article);
          });
          req.catch(function (err) {
            console.log(err);
          });

      } else {

        var req1 = $http.get('/api/articles');  

        req1.then(function (res) {
          scope.articles = res.data.articles;
        });
        req1.catch(function (err) {
          console.log(err);
        });

      }
        scope.awesomeThings = [
          'HTML5 Boilerplate',
          'AngularJS',
          'Karma'
        ];
      }]);

最后我的快递路线 - 但是自从api通话起作用后我不认为这是问题所在。

    /* GET users listing. */
    router.get('/', function(req, res, next) {  
        db.articles.find(function(err, articles){
            if(err){
              res.send(err);
            } 
              res.json({articles: articles});  
          }

    )});

    router.get('/:slug', function(req, res, next){
      console.log(req.params.slug); 
      db.articles.findOne({slug: req.params.slug}, function(err, article){

        if(err){
          res.send(err);
        } 
          res.json(article);

      }
    )});

非常感谢任何见解!享受您一天的其余时间:D

0 个答案:

没有答案