简体   繁体   English

AngularJS $ routeProvider不显示视图

[英]AngularJS $routeProvider not displaying view

Basically, when I navigate to http://localhost:9000 in my browser, I receive the content on the index.html page, but ng-view remains completely empty, despite '/' being pointed to my controller/view. 基本上,当我在浏览器中导航到http://localhost:9000时,我在index.html页面上收到了内容,但是ng-view仍然完全为空,尽管'/'指向了我的控制器/视图。

Upon first navigation, Angular does not add the /#/ to my url, and $location.path() reads an empty string. 在第一次航行,角度添加/#/给我的网址,和$location.path()读取一个空字符串。 After clicking any link, the about page for example, Angular adds /#/about to my url and the page loads normally. 单击任何链接后,例如“关于”页面的Angular将/#/about添加到我的网址中,并且该页面正常加载。 If for any reason, however, the page gets reloaded (I hit reload or live-reload updates), the /#/about remains in the url, but ng-view is an empty element. 但是,如果由于某种原因重新加载了页面(我点击了重新加载或实时重新加载更新),则/#/about仍保留在url中,但是ng-view是一个空元素。

The config: 配置:

.config(function ($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: 'views/main.html',
      controller: 'MainCtrl'
    })
    .when('/about', {
      templateUrl: 'views/about.html',
      controller: 'AboutCtrl'
    })
    .otherwise({
      redirectTo: '/'
    });
})

I included ngRoute in the app and the script has been added to the index. 我在应用程序中包括了ngRoute并且脚本已添加到索引中。

This app was working just fine before. 这个应用程式之前运作良好 I experimented with $locationProvider.html5Mode() a little and didn't like the results, so I did a hard reset to the git HEAD. 我稍微尝试了$locationProvider.html5Mode() ,但不喜欢结果,所以我对HEAD进行了硬重置。 After that, it has failed to work as intended on any machine, any browser. 此后,它无法在任何计算机,任何浏览器上正常运行。

I don't know what could be causing it or what else may be need to solve this. 我不知道是什么原因引起的,或者还需要其他什么解决方案。 Open to try anything that works at this point. 打开以尝试任何可行的方法。

EDIT: StackOverflow managed to remove a portion of my title. 编辑: StackOverflow设法删除了我的标题的一部分。 .-. .-。

我认为您应该能够在app.run中调用$ location.path('/'),以便在角度加载后进行导航。

Figured it out after a ton of digging, other people, and trial/error. 经过大量的挖掘,其他人和审判/错误后才弄清楚了。

If $scope.apply() is called to early, it can cancel the digestion that triggers on page load. 如果$scope.apply()提早调用,它可以取消在页面加载时触发的摘要。 Using $timeout over the method solved it. 在方法上使用$timeout解决了它。

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

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