简体   繁体   English

绝对角度路由和route / stateProvider

[英]angular absolute routes and route/stateProvider

I just have a problem with angular routes. 我只是角度路线有问题。 I have a singlePage Application with 4 different views. 我有一个具有4个不同视图的singlePage应用程序。 I'm using the ui.router for $stateProvider . 我将ui.router用于$stateProvider The main view is on route: https://testsite.com/ If I'm wanted to get to the page like: https://testsite.com/view1 i have an error. 主视图在途中: https://testsite.com/ : https://testsite.com/如果要进入以下页面: https://testsite.com/view1 : https://testsite.com/view1我遇到错误。 It is working if I'm on the main view and then changing the route on a click event like: 如果我在主视图上,然后在单击事件中更改路线,例如:

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

, but if i give the absolute url for the browser, it wont work. ,但是如果我提供浏览器的绝对URL,它将无法正常工作。

Here is my index.html: 这是我的index.html:

<body>
<div ui-view></div>
<script type="text/javascript" src="assets/javascript/libs/angular.min.js"></script>
<script type="text/javascript" src="assets/javascript/libs/angular-route.min.js"></script>
<script type="text/javascript" src="assets/javascript/libs/angular-ui-router.min.js"></script>
<script type="text/javascript" src="assets/javascript/app.js"></script>
</body>

view1: 厂景:

<div class="first-container">
//some content here
</div>

view2: 视图2:

<div class="second-conteiner">
//some content here
</div>

and my App.js: 和我的App.js:

var app = angular.module('SofticApp',['ngRoute', 'ui.router']);
app.config(function($routeProvider,$stateProvider, $urlRouterProvider, $locationProvider){
  $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  }).hashPrefix('!');
$stateProvider
  .state( '/', {
    url: '/',
    templateUrl: 'assets/views/view1.html',
    controller: 'viewFirstController'
  })
  .state( '/view2',{
    url: '/view2',
    templateUrl: 'assets/views/view2.html',
    controller: 'viewSecondController'
});

Thanks for your help! 谢谢你的帮助!

dont use the absolute url, use the path only 不要使用绝对网址,仅使用路径

alternatively use 替代使用

$state.go('view2')

notice that the state name should be 'view2' NOT '/view2' 请注意,状态名称应为'view2'而非'/ view2'

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

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