【问题标题】:ngresource open item in new pagengresource 在新页面中打开项目
【发布时间】:2017-07-14 18:08:19
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: angularjs node.js ngresource


    【解决方案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 以防万一。

    【讨论】:

    • 它说:Cannot GET /newpage/id=58 如何在 nodejs 服务器中添加到newpage/id 的路由?
    • 抱歉忘了说你必须把完整的 url 放在那里而不是只放那部分,使用 $location.absUrl()
    • 我仍然无法理解这一点。你能给我看一个例子吗?我编辑了这个问题。请检查。
    • 是的,等我回家
    • 嗨@Yaser,试过这个但还是不行。说我想包含一个模态,我如何在模态中传递动态数据?
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多