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: '/' });
}]);

这是这样做的方法,或者我只是迷路了? ^^

<小时 />

解决

我的解决方案对不起。我忘记了脚本角度路线,但我的其余脚本都可以工作。

在按钮中,您可以使用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>

最新更新