【问题标题】:cannot understand ui-router behaviour with Angular JS无法理解 Angular JS 的 ui-router 行为
【发布时间】:2015-05-25 06:24:05
【问题描述】:

我是 Angular 新手,正在尝试了解如何使用 ui-router。

我有以下设置

angular.module('formApp', ['ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.bootstrap.datetimepicker', 'ngResource'])

// configuring our routes 
// =============================================================================
.config(function($stateProvider, $urlRouterProvider) {

    $stateProvider
        // route to show our basic form (/form)
        .state('form', {
            url: '/',
            templateUrl: 'views/home.html',
            controller: 'formController'
        })
        // nested states 
        // each of these sections will have their own view
        // url will be nested (/form/profile)
       .state('form.date', {
            url: 'date',
            templateUrl: 'views/form-date.html'
        })
        // url will be /form/interests
        .state('form.address', {
            url: 'address',
            templateUrl: 'views/form-interests.html'
        })
        // url will be /form/payment
        .state('form.payment', {
            url: 'payment',
            templateUrl: 'views/form-payment.html'

        }).state('create', {
            url:'create',
            templateUrl: 'views/create.html',
            controller:'formController'
        });

    // catch all route
    // send users to the form page 
    $urlRouterProvider.otherwise('/');
})

当我最初加载页面时,这工作正常,但如果我键入其中一个路由,例如localhost:3000/date 我的看法不正确。当我到达 localhost:3000/form/date 时它也不起作用

我收到以下错误:Cannot GET /date 和开发者控制台中的 404 Not Found 错误。

谁能帮帮我?

这是我的 html 页面:

home.html

<div class="page-header text-center">

      <!-- the links to our nested states using relative paths -->
      <!-- add the active class if the state matches our ui-sref -->
         <div id="status-buttons" class="text-center">
            <a ui-sref-active="active" ui-sref=".date"><span>1</span> Date</a>
            <a ui-sref-active="active" ui-sref=".address"><span>2</span> Address</a>
            <a ui-sref-active="active" ui-sref=".payment"><span>3</span> Payment</a>
           </div>
 </div>            
   <form name="myform" id="signup-form" stripe:form="saveCustomer" novalidate>
       <!-- our nested state views will be injected here -->
       <div id="form-views" ui-view></div>
    </form>

谁能解释一下这里发生了什么?

【问题讨论】:

  • 一件事是ui-srefs 应该有像ui-sref="form.date" 这样的状态的全名。

标签: javascript angularjs node.js angular-ui-router url-routing


【解决方案1】:

试试localhost:3000/#/datelocalhost:3000/#/form/date

除非您指定$locationProvider.html5Mode(true);,否则您应该使用# root 进行角度应用。但是,Html5Mode 有一些限制,例如,您将无法使用$urlRouterProvider.otherwise(...);。检查this post。如果您是 Angular 新手,我不建议您使用 Html5Mode

【讨论】:

  • 谢谢 - 我讨厌那个 # 符号 - 如果我重定向节点服务器上的路由会重定向它吗?
  • 如果您有理由摆脱 # 使用 Html5Mode 并阅读答案中引用的帖子。它给出了如何处理服务器端的想法
猜你喜欢
  • 2015-04-16
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多