2016-03-20 195 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