繁体   English   中英

从URL AngularJS删除模板名称

[英]Remove template name from URL AngularJS

我正在使用angularJS $ routeProvider,

//模板

//路由

function ($routeProvider) {
       $routeProvider.
          when('/_profileView', {
              templateUrl: '_profileView.htm',
              controller: '_profileViewController'
          }).
          when('/', {
              templateUrl: '_homeView.htm',
              controller: '_homeViewController'
          }).
          when('/_homeView', {
              templateUrl: '_homeView.htm',
              controller: '_homeViewController'
          })} 

有没有办法像这样从Url中删除模板名称:Example.com/#/_homeView ----> Example.com/

如果您使用ui.router而不是ng-route ,它将解决您的问题。 例如:

angular.module('app',[ 'ui.router' ])
.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");
    $stateProvider.state('home', {
        url : "/",
        templateUrl : "_homeView.htm",
        controller: "_homeViewController"
    }).state('profile', {
        url : "/_profileView",
        templateUrl: "_profileView.htm",
        controller: "_profileViewController"
    })
 });

url属性中,如果只给出"/"则结果就是您所需要的。

关于使用ui.router而不是ng-route另一个说明 -我经历过,如果您想使用嵌套的ng-view -s,那么只有ui.router可能。

我希望这会有所帮助,加油!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM