实现角度ui路由器而不是ng-router

时间:2021-07-23 10:48:25

What am I doing wrong? I cannot make it work. The link of login and signin don't react as they are not links. I see the url of the login when I do the mouseover upon the link the following address localhost:8080/demo/index.html#/login. I click but it doesn't work at all.

我究竟做错了什么?我不能让它发挥作用。登录和登录的链接没有反应,因为它们不是链接。当我在链接上执行鼠标悬停时,我看到登录的URL,地址为localhost:8080 / demo / index.html#/ login。我点击但它根本不起作用。

At the beginning, I didn't realize that I needed to change the anchor attribute href to ui-sref, but when I use href, it redirects to login page completely without the index.html template structure.

一开始,我没有意识到我需要将锚属性href更改为ui-sref,但是当我使用href时,它会在没有index.html模板结构的情况下完全重定向到登录页面。

html - index.html

html - index.html

<!DOCTYPE html>
<html x-ng-app="demo">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
    <link href="../css/bootstrap.css" rel="stylesheet">
    <link href="../css/demo.css" rel="stylesheet">
    <link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">    
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-default" role="navigation" style="margin-bottom: 0px !important;">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header container-fluid">
        <a class="navbar-brand" href="index.html">Demo</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
          <li><a ui-sref="login">Log in</a></li>
          <li><a ui-sref="signup">Sign in</a></li>
        </ul>
      </div><!-- /.navbar-collapse -->
    </nav>
    <!-- /.container -->

    <!-- Page Header -->
    <!-- Set your background image for this header on the line below. -->
    <header class="intro-header" style="background-image: url('images/header.jpg')">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <div class="site-heading" style="height: 250px;">
                    </div>
                </div>
            </div>
        </div>
    </header>

   <!-- Main Content -->
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1" ui-view="layout">
                <!-- THIS IS JUST CONTENT WHEN ARRIVING TO THE INDEX PAGE -->
                <div class="post-preview">
                    <a href="post.html">
                        <h2 class="post-title">
                            Title
                        </h2>
                        <h3 class="post-subtitle">
                            blah blah
                        </h3>
                    </a>
                    <p class="post-meta">Posted by <a href="#">Start Bootstrap</a> on September 24, 2014</p>
                </div>
                <hr>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
        </div>
    </footer>

    <script src="../scripts/jquery-3.1.0.min.js"></script>
    <script src="../scripts/angular.min.js"></script>
    <script src="../scripts/angular-ui-router.min.js"></script>
    <script src="../scripts/bootstrap.min.js"></script>
    <script src="../scripts/ui-bootstrap-tpls-2.1.3.min.js"></script>       
    <script src="../scripts/demo.js"></script>
</body>
</html>

demo.js

var app = angular.module('demo', ['ui.router', 'ui.bootstrap']);
app.config(function($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider){
    $urlRouterProvider.otherwise('/');

    $urlMatcherFactoryProvider.caseInsensitive(true);
    $urlMatcherFactoryProvider.strictMode(false);   

    $stateProvider
    .state('index', {
        url: '/index',
        view:{
            'layout': {
                templateUrl: 'index.html'
            }
        },
        controller: ''
    })
    .state('login', {
        url: '/login',
        view: {
             'layout': {
                 templateUrl: 'login.html'
             }
        },
        controller: ''
    })
    .state('signup', {
        url: '/signup',
        view: {
            layout: {
                templateUrl: 'signup.html'
            }
        },
        controller: 'MyController'
    })
});

2 个解决方案

#1


1  

You're doing it completely wrong. First of all you need to have at least one ui-view which should be present inside index.html. What's more there won't be any state which is using index.html.

你完全错了。首先,你需要至少有一个ui-view应该出现在index.html中。更不会有任何使用index.html的州。

Ok, so here's an example how your index.html should look like:

好的,这里有一个例子,你的index.html应该是这样的:

index.html - navbar-brand destination was changed into ui-sref="home" which is your home/starting page. It's easier to use ui-sref which takes state name (home,login,signup) than href which takes url (#/,#/login,#/singup). Besides you'll have one place to change state destination (url) than searching for all href instances.

index.html - 导航栏品牌目标已更改为ui-sref =“home”,这是您的主页/起始页。使用状态名称(home,login,signup)的ui-sref比使用url(#/,#/ login,#/ singup)的href更容易。除了搜索所有href实例之外,你还有一个地方可以更改状态目的地(url)。

<!DOCTYPE html>
<html x-ng-app="demo">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
    <link href="../css/bootstrap.css" rel="stylesheet">
    <link href="../css/demo.css" rel="stylesheet">
    <link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">    
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-default" role="navigation" style="margin-bottom: 0px !important;">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header container-fluid">
        <a class="navbar-brand" ui-sref="home">Demo</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
          <li><a ui-sref="login">Log in</a></li>
          <li><a ui-sref="signup">Sign in</a></li>
        </ul>
      </div><!-- /.navbar-collapse -->
    </nav>
    <!-- /.container -->

    <!-- Page Header -->
    <!-- Set your background image for this header on the line below. -->
    <header class="intro-header" style="background-image: url('images/header.jpg')">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <div class="site-heading" style="height: 250px;">
                    </div>
                </div>
            </div>
        </div>
    </header>

   <!-- Main Content -->
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
               <!-- a place where your home.html, login.html and signup.html will be included -->
               <div ui-view=""></div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
        </div>
    </footer>

    <script src="../scripts/jquery-3.1.0.min.js"></script>
    <script src="../scripts/angular.min.js"></script>
    <script src="../scripts/angular-ui-router.min.js"></script>
    <script src="../scripts/bootstrap.min.js"></script>
    <script src="../scripts/ui-bootstrap-tpls-2.1.3.min.js"></script>       
    <script src="../scripts/demo.js"></script>
</body>
</html>

home.html - template which was previously included inside index.html

home.html - 以前包含在index.html中的模板

<div class="post-preview">
   <a href="post.html">
       <h2 class="post-title">
           Title
       </h2>
       <h3 class="post-subtitle">
           blah blah
       </h3>
   </a>
   <p class="post-meta">Posted by <a href="#">Start Bootstrap</a> on September 24, 2014</p>
</div>

demo.js - here you can find all states definition which are going to be placed in place of ui-view directive.

demo.js - 在这里你可以找到所有的状态定义,它们将代替ui-view指令。

var app = angular.module('demo', ['ui.router', 'ui.bootstrap']);
app.config(function($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider){
    $urlRouterProvider.otherwise('/');

    $urlMatcherFactoryProvider.caseInsensitive(true);
    $urlMatcherFactoryProvider.strictMode(false);   

    $stateProvider
    .state('home', {
        url: '/',
        templateUrl: 'home.html',
        controller: function(){}
    })
    .state('login', {
        url: '/login',
        templateUrl: 'login.html',
        controller: function(){}
    })
    .state('signup', {
        url: '/signup',
        templateUrl: 'signup.html',
        controller: 'MyController'
    })
});

#2


0  

I was able to get your code to work when i removed the 'layout' portion. also, i dont think you need the index page in the routing.

当我删除'layout'部分时,我能够让你的代码工作。另外,我认为你不需要路由中的索引页面。

      <div  ui-view></div>

       $stateProvider.state('login', {
            url: '/login',
            templateUrl: 'login.html'
        })
        .state('signup', {
            url: '/signup',
            templateUrl: 'signup.html'
        })

#1


1  

You're doing it completely wrong. First of all you need to have at least one ui-view which should be present inside index.html. What's more there won't be any state which is using index.html.

你完全错了。首先,你需要至少有一个ui-view应该出现在index.html中。更不会有任何使用index.html的州。

Ok, so here's an example how your index.html should look like:

好的,这里有一个例子,你的index.html应该是这样的:

index.html - navbar-brand destination was changed into ui-sref="home" which is your home/starting page. It's easier to use ui-sref which takes state name (home,login,signup) than href which takes url (#/,#/login,#/singup). Besides you'll have one place to change state destination (url) than searching for all href instances.

index.html - 导航栏品牌目标已更改为ui-sref =“home”,这是您的主页/起始页。使用状态名称(home,login,signup)的ui-sref比使用url(#/,#/ login,#/ singup)的href更容易。除了搜索所有href实例之外,你还有一个地方可以更改状态目的地(url)。

<!DOCTYPE html>
<html x-ng-app="demo">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
    <link href="../css/bootstrap.css" rel="stylesheet">
    <link href="../css/demo.css" rel="stylesheet">
    <link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">    
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-default" role="navigation" style="margin-bottom: 0px !important;">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header container-fluid">
        <a class="navbar-brand" ui-sref="home">Demo</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
          <li><a ui-sref="login">Log in</a></li>
          <li><a ui-sref="signup">Sign in</a></li>
        </ul>
      </div><!-- /.navbar-collapse -->
    </nav>
    <!-- /.container -->

    <!-- Page Header -->
    <!-- Set your background image for this header on the line below. -->
    <header class="intro-header" style="background-image: url('images/header.jpg')">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <div class="site-heading" style="height: 250px;">
                    </div>
                </div>
            </div>
        </div>
    </header>

   <!-- Main Content -->
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
               <!-- a place where your home.html, login.html and signup.html will be included -->
               <div ui-view=""></div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
        </div>
    </footer>

    <script src="../scripts/jquery-3.1.0.min.js"></script>
    <script src="../scripts/angular.min.js"></script>
    <script src="../scripts/angular-ui-router.min.js"></script>
    <script src="../scripts/bootstrap.min.js"></script>
    <script src="../scripts/ui-bootstrap-tpls-2.1.3.min.js"></script>       
    <script src="../scripts/demo.js"></script>
</body>
</html>

home.html - template which was previously included inside index.html

home.html - 以前包含在index.html中的模板

<div class="post-preview">
   <a href="post.html">
       <h2 class="post-title">
           Title
       </h2>
       <h3 class="post-subtitle">
           blah blah
       </h3>
   </a>
   <p class="post-meta">Posted by <a href="#">Start Bootstrap</a> on September 24, 2014</p>
</div>

demo.js - here you can find all states definition which are going to be placed in place of ui-view directive.

demo.js - 在这里你可以找到所有的状态定义,它们将代替ui-view指令。

var app = angular.module('demo', ['ui.router', 'ui.bootstrap']);
app.config(function($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider){
    $urlRouterProvider.otherwise('/');

    $urlMatcherFactoryProvider.caseInsensitive(true);
    $urlMatcherFactoryProvider.strictMode(false);   

    $stateProvider
    .state('home', {
        url: '/',
        templateUrl: 'home.html',
        controller: function(){}
    })
    .state('login', {
        url: '/login',
        templateUrl: 'login.html',
        controller: function(){}
    })
    .state('signup', {
        url: '/signup',
        templateUrl: 'signup.html',
        controller: 'MyController'
    })
});

#2


0  

I was able to get your code to work when i removed the 'layout' portion. also, i dont think you need the index page in the routing.

当我删除'layout'部分时,我能够让你的代码工作。另外,我认为你不需要路由中的索引页面。

      <div  ui-view></div>

       $stateProvider.state('login', {
            url: '/login',
            templateUrl: 'login.html'
        })
        .state('signup', {
            url: '/signup',
            templateUrl: 'signup.html'
        })