2017-02-24 1 views
0

Nodejs 및 Angular에 Rest API를 만들었습니다. 내 각도 응용 프로그램에서ngresource 새 페이지에 항목 열기

는 내가 가지고 : nodejs에서

app.factory('User', function($resource) { 
    return $resource('/api/users/:id', { id: '@_id' }); 
}); 


app.controller("myctrl", function($scope, $http, UserService, $location){ 
    $scope.users = User.query(); 
     $scope.getData = function(userID){ 
      $location.absUrl("/api/students/"+userID); 
     } 
    }); 

:

app.use('/api', require('./routes/api')); 

내가 필요한 모든 정보를 나열 할 수 있어요. 이제 사용자를 새 페이지에서 열어 보겠습니다. 나는 사용자가 클릭하면 그래서, 난이 :

편집 : 사용자 이름을 클릭

<base href="/"> //in my html head 
<p><ng-click="getData(user.id)">username</p> 

, 아무 일도 발생하지합니다. 콘솔 로그에도 오류가 없습니다.

편집

$location.url("/api/students/"+userID);는 주소 표시 줄에 적절한 위치를두고 있지만 페이지가 동일하게 유지됩니다. 그래서 사용했습니다

$window.location.assign("/api/students/"+userID); 

$window.location.assign을 사용해야하는지 잘 모르겠습니다. 이 작품과 나는 모든 적절한 세부 사항을 얻을. 유일한 문제는 그 모든 것이 JSON에 있다는 것입니다. 데이터를 표시하기 위해 {{}}을 어떻게 그리고 어디에서 사용해야합니까? 또한 출력에 사용자 정의 html을 추가하려고합니다.

감사합니다.

답변

1

a 태그에서 대상 공백을 사용할 수 있습니다. 그 외에도 $ location을 삽입하거나 window.location을 사용하여 전체 경로를 가져와야하므로 링크가 작동하지 않습니다. 짧은

그래서 긴 이야기 :

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope, $location) { 
 
    
 
    $scope.userID = 10; 
 
    $scope.baseUrl = $location.protocol() + "://" + $location.host() + ":" + $location.port(); 
 
    
 
    $scope.fullUrl = $scope.baseUrl + '/api/students/' + $scope.userID; 
 
    
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="plunker"> 
 
    <div ng-controller="MainCtrl"> 
 
    <p>Hello!</p> 
 
    
 
    <a target="_blank" href="{{baseUrl + '/api/students/' + userID}}" >Go to new window</a> 
 
    <br/> 
 
    <a target="_blank" href="{{fullUrl}}" >Go to new window</a> 
 
    </div> 
 

 
</div>
그리고 단지의 경우에 plunker.

+0

그것은 말합니다 :'GET/newpage/id = 58' nodejs 서버에서'newpage/id'에 경로를 어떻게 추가합니까? – Somename

+0

죄송합니다. 그 부분 대신에 전체 URL을 넣어야한다는 점을 잊어 버렸습니다. $ location.absUrl()을 사용하십시오. – Yaser

+0

아직도 내 머리를 감쌀 수 없습니다. 예를 보여 주실 수 있겠습니까? 나는 그 질문을 편집했다. 확인해주십시오. – Somename