Angular JS页面一开始无法正常加载

Angular JS page not loading properly in beginning

本文关键字:常加载 加载 JS 一开始 Angular      更新时间:2023-09-26

这是我的angularJS应用的配置。

sampleApp.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/Home");
    $stateProvider
    .state('Product', {
        templateUrl: "templates/Products.html",
        url: '/Product',
        controller: 'MainCtrl2 as pc'
    })
    .state('Login', {
        templateUrl: "templates/Login.html",
        url: "/Login",
        controller: 'loginCtrl'
    })
    .state('SignUp', {
        templateUrl: "templates/SignUp.html",
        url: "/SignUp",
        controller: 'SignUpController'
    })
    .state('Home', {
        templateUrl: "templates/Home.html",
        url: "/Home"
    })
    .state('Feedback', {
        templateUrl: "templates/Feedback.html",
        url: "/Feedback"
    })
    ;
});

问题是当我输入我的域名说example.com,我希望它转换成http://example.com/#/Home,但有时(6/10),example.com不改变为http://example.com/#/Home,所以应用程序不能正常加载。

一旦我点击卸载应用程序中的任何链接,它就会开始正常工作。

如何确保每次页面加载正确

而不是直接添加

 $urlRouterProvider.otherwise("/Home");

尝试使用以下路由

 $urlRouterProvider
   .when('/', function ($state) {
       $state.go('Home');
   }).otherwise('/Home');

我认为这将解决你的问题:http://niclassahlin.com/2014/05/31/kickstarting-angular-ui-router/

app.run(['$state', function ($state) {}])

本质上,你需要在应用运行时启动路由器,以确保路由被初始化。不知道为什么你当前的应用程序偶尔会正确路由,而没有看到完整的代码。