簡體   English   中英

URL angularjs的重定向

[英]Redirection of url angularjs

我正在構建一個應用程序,但有一個小問題。

對於索引,當我單擊帶有jQuery的按鈕時,我使用history.pushstate更改了URL。 例如, www.mysite.com變為www.mysite.com/something而無需重新加載頁面。 但是,如果加載此頁面,我找不到www.mysite.com因為“內容”不存在。

而且我不知道它們之間的聯系如何。

我試圖做到這一點,但它不適用於AngularJS。

var myApp = angular.module('photo', ['ngRoute']);


myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'machin.html'
    });
    $routeProvider.when('/something-one', {
        templateUrl: 'machin.html'
    });
    $routeProvider.when('/something-two', {
        templateUrl: 'machin.html'
    });
    $routeProvider.otherwise({ redirectTo: '/' });
}]);

這是這樣做的方法,還是我迷路了? ^^


解決了

我的解決方案對不起所有人。 我忘記了腳本angular-route,但其余腳本仍然有效。

在您的按鈕中,您可以使用ng-click而不是jQuery綁定事件。

<button ng-click="goto('/something')">Go to something</button>

在您的控制器中:

$scope.goto = function(url) {
   $location.path(url);
}

當您使用Jquery時,您超出了范圍。 因此,角度不會知道URL的更改。 在這種情況下,您可能需要觸發digest循環。

你可以試試這個

var myApp = angular.module('photo', ['ngRoute']);
myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'machin.html'
    })
    .when('/something-one', {
        templateUrl: 'machin.html'
    })
    .when('/something-two', {
        templateUrl: 'machin.html'
    })
    .otherwise({ redirectTo: '/' });
}]);

並在您的html中

<a href="#/something-one">Go to something one</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM