UI Router states. not setting right by changing the url. - angular ui router

时间:2017-08-05 10:58:05

标签: javascript angularjs angular-ui-router

I past many solutions and tutorials already, without found a right answer. So I hope I don't make a duplicate and give us a nice little challenge here

use

angular

ui router

Problem

I have some states and those states have their own unique url. I click on link (ui-sref) the state and url changing correctly. however when I set the url (for example "localhost:8080/user") and click 'Enter' on my keybord. the current state change to some 'empty state' without nothing even if the url setting correctly

Code

config :

'use strict';

angular.module('divohon-app').config(function ($stateProvider,  $urlRouterProvider, $locationProvider) {


    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });

    $stateProvider
        .state("user-home", {
            url: "user",
            templateUrl: "/resources/html/template/user-home.html",
            controller: "UserHome",
            controllerAs: "uhCtrl"
        })
        .state("admin-home", {
            url: "admin",
            templateUrl: "/resources/html/template/admin-home.html",
            controller: "AdminHome",
            controllerAs: "ahCtrl"
        })
        .state("login", {
            url: "login",
            templateUrl: "/resources/html/template/login.html",
            controller: "LoginController",
            controllerAs: "lCtrl"
        });
});

Thank you for any help. or reference.

1 个答案:

答案 0 :(得分:0)

您只需将每个州的网址属性分别更新为/user/admin/login。它应该解决问题。

$stateProvider
.state("user-home", {
    url: "/user",
    templateUrl: "/resources/html/template/user-home.html",
    controller: "UserHome",
    controllerAs: "uhCtrl"
})
.state("admin-home", {
    url: "/admin",
    templateUrl: "/resources/html/template/admin-home.html",
    controller: "AdminHome",
    controllerAs: "ahCtrl"
})
.state("login", {
    url: "/login",
    templateUrl: "/resources/html/template/login.html",
    controller: "LoginController",
    controllerAs: "lCtrl"
});

经过上述更改后,localhost:8080 / user应该正确显示视图。