2016-03-20 198 views
0

我遇到了从我的前端调用路由到后端的问题。Expressjs从angularjs路由路由

我使用的客户端上angularjs $状态,这样的例子网址是

http://localhost:1234/areas/customer/edit/56e2f6345968f8b812052694

然而,当我调用服务器sideusing $种源查询这条路线如下调用不正确的服务器端路线。

我厂包含使用$资源调用服务器端

getCustomerByID: function(id, callback){ 
      var cb = callback || angular.noop; 

      return Customer.query(id, function(success){ 
      return cb(success); 
      }, function(error){ 
      return cb(error); 
      }).$promise; 
     } 

客户是如下

angular.module('xxx') 
    .factory('Customer', function ($resource) { 
    return $resource('/api/customer/:id/:controller', { id: '@_id' }) 
    }); 

服务器路由服务是为如下的GET

的方法
router.get('/', controller.index); 
router.get('/:id', controller.show); 

我需要用id命中路线,但它似乎击中了第一个

任何想法可能是

干杯

回答

0

路线按顺序处理,交换以下455元:

router.get('/', controller.index); 
router.get('/:id', controller.show); 

要这样:

router.get('/:id', controller.show); 
router.get('/', controller.index); 

出现这种情况因为/api/customer/:id/之类的请求也只会匹配/api/customer,因此它为什么捕捉到第一条路线。

+0

是啊试过,但没有快乐不应该重新启动grunt服务器? – tjhack

+0

是的,尝试重新启动 - 上面的路线应该可以工作。 –

+0

仍然没有快乐。当传递id到查询时,我试图将它作为一个数组[{id:id}]传递,但那不起作用 – tjhack