简体   繁体   English

AngularJs重新加载页面而不是仅加载视图

[英]AngularJs reload page instead only load view

I have a simple app in AgularJS. 我在AgularJS中有一个简单的应用程序。 I need to load view in route 我需要在路线中加载视图

$routeProvider.when('/articles/:url', {
  templateUrl: 'partials/article.html',
  controller: ArticleCtrl
});

But if i click on Page reloads and after reload Angular try to load partials/article.html view, but fails on error " NetworkError: 404 Not Found - http://localhost:3000/clanky/partials/article.html " I know that I can use "../partials/article.html" insted "partials/article.html", but I mean that isn't the core of problem. 但是,如果我单击页面重新加载,然后在重新加载Angular之后,尝试加载partials / article.html视图,但失败并显示错误“ NetworkError: 404 Not Found - http://localhost:3000/clanky/partials/article.html ”,我知道我可以使用“ ../partials/article.html”插入“ partials / article.html”,但这并不是问题的核心。

There are my routes: 有我的路线:

blog.config([
  '$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);
    $routeProvider.when('/', {
      templateUrl: 'partials/main.html',
      controller: MainCtrl
    });
    $routeProvider.when('/login', {
      templateUrl: 'partials/login.html',
      controller: LoginCtrl
    });
    $routeProvider.when('/backend', {
      templateUrl: 'partials/backend.html',
      controller: BackendCtrl
    });
    $routeProvider.when('/articles/:url', {
      templateUrl: 'partials/article.html',
      controller: ArticleCtrl
    });
    return $routeProvider.otherwise({
      redirectTo: '/'
    });
  }
]);

PS If I try go to the any other route, for example, login, it partialy works, but reload is still here. 附注:如果我尝试使用其他任何路径,例如登录,则部分可行,但重装仍在此处。

Thanks for yours answers 谢谢你的回答

seems that I've got the same problem as you. 看来我和您有同样的问题。

I don't know how to handle this but I think that's because the page was reloaded . 我不知道该如何处理,但我认为是因为页面已重新加载

When you are not setting the html5Mode to true , then will be # at the url, and if you reload the page, the # will track the information to the $scope to work with the htmlHistory api so that the app can work properly even with the page refresh. 当您未将html5Mode设置为true ,URL处将是# ,并且如果您重新加载页面,则#会将信息跟踪到$scope以与htmlHistory api配合使用,以便该应用程序即使在页面刷新。

But once you set the html5Mode to true , then there's no # to store any $scope information, and when the page reload, angular can not found the accordingly scope so it return 404 . 但是,一旦将html5Mode设置为true ,便没有#存储任何$scope信息,并且当页面重新加载时,angular找不到相应的作用域,因此返回404

You can check $location section on angularjs guide , it has a section explaining why this happen. 您可以在angularjs guide上查看$ location部分 ,其中有一个部分解释了为什么发生这种情况。

Page reload navigation 页面重新加载导航

The $location service allows you to change only the URL; $ location服务仅允许您更改URL。 it does not allow you to reload the page. 它不允许您重新加载页面。 When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href. 当您需要更改URL并重新加载页面或导航到其他页面时,请使用较低级别的API,$ window.location.href。

Hope this can be helpful for you, and if you got any better answer, please let me know, I'm also waiting for the right way to solve it. 希望这对您有所帮助,如果您有更好的答案,请告诉我,我也在等待正确的解决方案。

Start your templateUrl with a slash: 用斜杠启动templateUrl:

templateUrl: '/partials/article.html'

instead of 代替

templateUrl: 'partials/article.html'

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

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