2014-02-27 4 views
28

단순한 AngularJS 앱을 제작하고 있지만 가능한 경우 라우팅을 사용하지 않고 서버에서 개별 Angular 페이지를 다시 제공합니다. 본질적으로, '위젯'의 레이아웃과 기능에만 Angular를 사용하고 페이지간에 이동하는 경우에는 Angular를 사용하지 마십시오. ,각도 - 컨트롤러의 URL 경로에서 ID를 반환하십시오.

/Tickets/:id 

내 컨트롤러에서하고 싶은 모든, URL에서 ID를 얻을 수 있습니다 : 그와

내가 보낸 시간 때문에, 현재의 URL에서 ID를 얻으려고 노력했습니다 말했다 그것을 공장으로 전달하십시오. (아래는 의사 코드를 사용한 예입니다)

app.controller('ticketController', ['$scope', '$route', 
    function ($scope, $route, $location) { 
     //Get ID out of current URL 
     var currentId = someFunction().id; 
     factory.callFactory(ID); 
    }]); 

경로를 만들려고했으나 반환 된 개체가 ID를 가져 오는 방법을 제공하지 않는 것 같습니다.

app.config(['$routeProvider', function ($routeProvider) { 
    $routeProvider 
    .when('/tickets/:id', 
     { 
      templateUrl: '/partials/edit', 
      controller: 'ticketController' 
     } 
    ); 
}]); 

내가 잘못하고 있는지 확실하지 않습니다.

This doesn't seem to work for me

This also doesn't seem to work, but I may be doing something wrong

답변

42

대신 $routeParams을 시도해보십시오

app.controller('ticketController', ['$scope', '$routeParams', 
    function ($scope, $routeParams) { 
     //Get ID out of current URL 
     var currentId = $routeParams.id; 
}]); 
+6

을 당신은이 작업을 할 수 ngRoute를 설치해야합니다 : https : //docs.a ngularjs.org/api/ngRoute – Magne

+0

$ location.path ("/ products /"+ category.id)를 사용하면 어떻게 되나요? ? –

18

그렇게 $stateParams을 시도 ui-router를 사용하는 경우 :

app.controller('ticketController', ['$scope', '$stateParams', 
    function ($scope, $stateParams) { 
     //Get ID out of current URL 
     var currentId = $stateParams.id; 
}]); 
+0

$ location.path ("/ products /"+ category.id)를 사용하면 어떻게 얻을 수 있습니까? ? –

관련 문제