ngresource 在新页面打开项目

ngresource open item in new page

我在 Nodejs 和 Angular 中创建了一个 Rest API。

在我的 Angular 应用程序中,我得到了:

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);
       }
  });

在 nodejs 中:

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 中。如何以及在何处使用 angular {{}} 来显示数据?另外,我还想在该输出中添加一些自定义 html。

谢谢。

您可以在 a 标签中使用目标空白。除此之外,您还需要注入 $location 或仅使用 window.location 来获取完整路径,否则 link 将不起作用。

长话短说:

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 以防万一。