使用AngularJS排除URL中的路径

时间:2015-01-03 18:37:55

标签: angularjs angular-ui-router

我的角度应用程序有多个页面,用户可以访问,我想隐藏所有其他网址,只显示用户的基本网址。所以想象一下我的基本网址是:www.example.com,我还有其他网页,如关于,联系我们等。目前,当用户点击关于时,网址会更改为www.example.com/about。角度是否可以不添加“/ about”?这是否可以使用角度js?另外,我一直在寻找解决方案,并尝试过ui-router.js,这个模块也可以吗?

1 个答案:

答案 0 :(得分:11)

如果您使用的是ui-router,那么您可以定义状态而无需指定网址,如

myApp.config(function($stateProvider, $urlRouterProvider) {
    //
    // For any unmatched url, redirect to /state1
    $urlRouterProvider.otherwise("/state1");
    //
    // Now set up the states
    $stateProvider
        .state('state1', {
            url: "/state1",
            templateUrl: "state1.html",
            controller: 'Controller3'
        })
        .state('state2', {
            templateUrl: "state2.html",
            controller: 'Controller2'
        })
        .state('state3', {
            templateUrl: "state3.html",
            controller: 'Controller3'
        })
});

因此,默认情况下,url将为/ state1。您可以在模板中使用ui-sref指令(如ui-sref="state2")或在控制器中使用$ state服务(如$state.go('state2'))来实现导航。