使用nodeJs连接到Mongo数据库时阻止页面重新加载

时间:2018-01-29 06:31:00

标签: node.js mongodb angular

我在这里尝试使用“_id”将两个页面链接在一起,并将其保存在url中。但是当我点击角度href链接在nodejs中链接时,我正在重新加载我的页面。这是我的代码 angularJs

 <a style="text-decoration:none;" href="/ordercustomer/{{x._id}}">    {{ x.orders.length ||  0 }} order </a>

节点js

app.get('/ordercustomer/:id', function(req, res, next){
  console.log(req.params.id);

        console.log('I received another get request');
       dbConn.then(function(db) {
       var dd = db.db("customermanagement");
   dd.collection('customers').findOne({_id:ObjectId(req.params.id)},function(err, results) {

     console.log(results);
     res.redirect('/index1.html#!/ordercustomer/'+req.params.id)
      });

    });
});

其中index.html是角度路由中的主页面。 app.js

 when('/ordercustomer/:userid1', {

                   templateUrl: 'ordercustomer.html',
               controller: 'ordercustomerCtrl'
              }).

1 个答案:

答案 0 :(得分:0)

您的节点获取api将是:

app.get('/ordercustomer/:id', function(req, res, next){
   dbConn.then(function(db) {
     var dd = db.db("customermanagement");
     dd.collection('customers').findOne({_id:ObjectId(req.params.id)},
      function(err, results) {
       res.status(200).send({ status: 1000 }); //and other json fileds whatever you want to send back to client
     });
   });
 });

在你的角度客户端控制器中:

$scope.yourSubmitFunction = function(id){
   $http({
       url : '/ordercustomer/' + id,
       method: 'GET'
   }).then(function(result){
        //this is your result from node api
        if(result.status === 1000){
           //use $window or $location for redirection to your page
        }
   })
};