简体   繁体   English

MEAN堆栈Angular显示Express中的对象,但不显示HTML

[英]MEAN stack Angular shows Object from Express but not HTML

I am trying to make a Blog site and I am stuck at creating single 'post' view page. 我试图建立一个Blog网站,但我坚持创建单个“发布”视图页面。 When I got /post/id page from href everything works but when I refresh the page manually or share the link I get page like this: 当我从href获得/ post / id页面时,一切正常,但是当我手动刷新页面或共享链接时,我得到这样的页面:

{"_id":"56fa7443c1542bdc3bdb3857","user":{"_id":"56fa6b0cc83017643c50c5ad",

Been stuck at this for days, how can I fix this? 被这个问题困扰了几天,我该如何解决?

Express code (./app/backend/routes/post.js) 快速代码(./app/backend/routes/post.js)

router.get('/:id', function (req, res) {
    Model.findById(req.params.id, function(err, post){
        return res.status(200).send(post);
    })
    .populate('user');
});

Angular PostCtrl code (./app/frontend/js/controller/post.js) Angular PostCtrl代码(./app/frontend/js/controller/post.js)

$http.get('/post/'+$routeParams.id).success(function (post) {
    post.text = $sce.trustAsHtml(post.text);
    $scope.post = post;

}).error(function (error, status) {
    $scope.errorMessage = error + ' (code:' + status + ')';
});

Routes (./app/frontend/js/app.js) 路由(./app/frontend/js/app.js)

$routeProvider
.when('/', {
  templateUrl: 'views/main.html',
  controller: 'MainCtrl'
})
.when('/post/:id', {
  templateUrl: 'views/post.html',
  controller: 'PostCtrl'
})

I got it fixed. 我把它修好了。 I had to change Angular routing from .when('/post/:id' to .when('/blog/:id' and change the hrefs to /blog/:id . 我不得不将Angular路由从.when('/post/:id'更改为.when .when('/blog/:id' ,并将hrefs更改为/blog/:id

It seems angular and express routing cant be the same. 似乎角路由和快速路由不能相同。 Express seems to still send the data to /post/:id and angular gets the data from there and displays it to /blog/:id now. Express似乎仍将数据发送到/post/:id而angular从那里获取数据并将其显示到/blog/:id After refreshing the page everything seems to work fine now. 刷新页面后,一切似乎都可以正常工作了。 Just need to hide sensitive data in /post/:id now or find a way users can get access to /post/:id . 现在只需要在/post/:id隐藏敏感数据,或者找到一种用户可以访问/post/:id

I am trying to understand your code and this is what I could come up with 我正在尝试理解您的代码,这就是我能想到的

// this is how the route should be using populate
router.get('/post/:id', function (req, res) {
  Model.findOne({_id: req.params.id}).populate('user').exec(function (error, post) { 
    if (post) {
      res.status(200).json(post);
    } else {  
      // handle error here...
    }
  })
});

Every other thing looks okay. 其他的一切看起来还不错。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM