AngularJS $ routeProvider问题

时间:2015-07-25 04:45:50

标签: javascript angularjs

当我点击Home链接时,会执行goTo功能。但是,它会将我重定向到一个空白页面,如下所示:

page

当我点击浏览器的back按钮时,它会将我重定向到我想要转到的页面(firstPage)。

enter image description here

有关我做错的任何建议吗?

HTML:

<footer ng-controller = "footerLink">
        <a href = "#"  ng-click="goTo('/firstPage')"> Home </a>
        <a href = "#"> About us </a>
        <a href = "#"> Our Team </a>
        <a href = "#"> Blog </a>
        <a href = "#"> Services </a>
        <a href = "#"> Portfolio </a>
        <a href = "#"> Contact </a>
        <a href = "#"> Sitemap </a>
    </footer>

JS

angular.module('myApp.view1', ['ngRoute'])
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider.
            when('/view1', {
                templateUrl: 'view1/view1.html',
                controller: 'View1Ctrl'
        }).
            when('/buttonView', {
                templateUrl: 'buttonView/buttonView.html',
                controller: 'buttonViewCtrl'
        }).
            when('/firstPage', {
                templateUrl: 'view2/view2.html',
                controller: 'footerLink'
        }).
            when('/home', {
                templateUrl: 'view1/view1.html',
                controller: 'logo'
        });
    }])

.controller('footerLink',  ['$scope', '$location', function($scope, $location) {
    $scope.goTo = function (url) {
      $location.path(url);
     };
}])

更新

我直接将anchor重定向到firstPage路由,而不是调用另一个函数。以下是我改变它的工作原理:

<footer ng-controller = "footerLink">
        <a href = "#/firstPage"> Home </a>
        <a href = "#"> About us </a>
        <a href = "#"> Our Team </a>
        <a href = "#"> Blog </a>
        <a href = "#"> Services </a>
        <a href = "#"> Portfolio </a>
        <a href = "#"> Contact </a>
        <a href = "#"> Sitemap </a>
    </footer> 

1 个答案:

答案 0 :(得分:2)

这是因为您没有重定向规则或路由配置的&#39; /&#39;如果没有匹配的路由,您可以将用户重定向到主页。

angular.module('myApp.view1', ['ngRoute'])
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider.
            when('/view1', {
                templateUrl: 'view1/view1.html',
                controller: 'View1Ctrl'
        }).
            when('/buttonView', {
                templateUrl: 'buttonView/buttonView.html',
                controller: 'buttonViewCtrl'
        }).
            when('/firstPage', {
                templateUrl: 'view2/view2.html',
                controller: 'footerLink'
        }).
            when('/home', {
                templateUrl: 'view1/view1.html',
                controller: 'logo'
        }).otherwise({redirectTo: '/home'});
    }])

.controller('footerLink',  ['$scope', '$location', function($scope, $location) {
    $scope.goTo = function (url) {
      $location.path(url);
     }; // this function is not really required
}]);

而不是创建另一个重定向用户的功能,你可以使用在html上使用href,因为你没有在那里进行任何其他处理,除了重定向用户。

<footer ng-controller = "footerLink">
    <a href = "/firstPage"> Home </a>
    <a href = "#"> About us </a>
    <a href = "#"> Our Team </a>
    <a href = "#"> Blog </a>
    <a href = "#"> Services </a>
    <a href = "#"> Portfolio </a>
    <a href = "#"> Contact </a>
    <a href = "#"> Sitemap </a>
</footer>