简体   繁体   English

Angular JS页面开始时未正确加载

[英]Angular JS page not loading properly in beginning

This is my configuration for angularJS application. 这是我对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"
    })
    ;
});

Problem is when i am typing my domain name say example.com, i want it to get convert into http://example.com/#/Home , but sometimes (6 out of 10), example.com not changing to http://example.com/#/Home , so application is not loading properly. 问题是当我输入我的域名,例如example.com时,我希望它转换为http://example.com/#/Home ,但是有时(10之6)不能将example.com更改为http: //example.com/#/Home ,因此应用程序无法正确加载。

Once i click on any of the link in unloaded application it starts working fine. 一旦我单击了已卸载应用程序中的任何链接,它就会开始正常工作。

How to make sure every time page loads properly. 如何确保每次页面加载正确。

Rather then adding otherwise directly 而不是直接添加

 $urlRouterProvider.otherwise("/Home");

try using following route 尝试使用以下路线

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

Here's what I believe will solve your issue: http://niclassahlin.com/2014/05/31/kickstarting-angular-ui-router/ 我相信这可以解决您的问题: http : //niclassahlin.com/2014/05/31/kickstarting-angular-ui-router/

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

Essentially you need to kickstart the router when your app runs, ensuring the otherwise route is initialised. 本质上,您需要在应用运行时启动路由器,以确保初始化其他路由。 Not sure why your current app would sporadically route correctly without seeing the full code. 不知道为什么您当前的应用偶尔会正确路由而不看到完整的代码。

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

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