【问题标题】:Redirecting to another page using a button click使用按钮单击重定向到另一个页面
【发布时间】:2019-09-15 18:18:06
【问题描述】:

我想在单击按钮时重定向到另一个网页。请找到以下代码

js

app.controller('myCtrl', ['$scope', '$location', function($scope, $location) {

$scope.redirect = function()
{
console.log($location.url());

};

}

我可以使用下面的 $location.url() 获取当前 url

http://IP:Port/?myId=this-is-my-test-page

我想要做的是当我点击一个按钮时我想重定向到以下页面

http://IP:Port/?myId=this-is-my-test-page2

我想编辑控制器内的现有 url,而不用硬编码整个 url,因为 IP 和端口可以更改。我怎样才能做到这一点?

虽然我用过$location.path("http://IP:Port/?myId=this-is-my-test-page2"); 它的作用是按如下方式附加到当前网址

http://IP:Port/?myId=this-is-my-test-page2#http://IP:Port/?myId=this-is-my-test-page2

如何重定向到所需的页面?

【问题讨论】:

  • 你可能想参考Angular的documentation。我认为你应该定义routes
  • 我可以看到路线吗?
  • 我还没有创建任何路线。不使用路线我不能这样做吗?

标签: html angularjs url-redirection


【解决方案1】:

我有一个不太相似的问题,我必须将变量转换为字符串,然后修改字符串。然后转换回正确的数据类型。

【讨论】:

【解决方案2】:

Angular 有自己的路由。 # 右边的一切都是 Angular 路由(客户端路由)。 # 号左侧的所有内容都是您的 MVC/WebApi 路由(服务器端路由)。这是单页应用程序的本质。

如果您想更改服务器端路由,您需要在服务器端进行重定向,因此请向服务器请求更改 url。如果要更改客户端(Angular)路由,则需要在 Angular 代码中使用“$location.path('')”。请注意,您无法更改客户端 # 符号左侧的任何内容。

您正在尝试更改 url 的服务器端部分,因此您需要向服务器发出请求以更改 url。

有关两者的更多信息,请参阅this question

【讨论】:

    【解决方案3】:

    我尝试使用 $window.location 并且成功了。其他方法均无效。

    app.controller('myCtrl', ['$scope', '$window', function($scope, $window) {
    
    $scope.redirect = function()
    {
    $window.location = "http://IP:Port/......";
    
    };
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2017-12-06
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2014-07-21
      相关资源
      最近更新 更多