【问题标题】:Angular-Fullstack + UI router isn't routing properlyAngular-Fullstack + UI 路由器没有正确路由
【发布时间】:2015-04-14 21:45:51
【问题描述】:

我正在尝试使用 UIrouter 和当前没有 mongoDB 的 yeoman-fullstack 生成器来路由我的应用程序。在我的问题的这个示例中,一切都是默认测试并确保 UI 路由器正常工作。

主要是因为我不理解和遗漏了一些愚蠢和愚蠢的东西,所以我道歉。

https://github.com/DaftMonk/generator-angular-fullstack

我正在尝试使用

在 client/app/app.js 中编辑我的路由

'use strict';

angular.module('sr388App', [
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ui.router',
  'ui.bootstrap'
])
  .config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
    $urlRouterProvider
      .otherwise('/');

    $stateProvider

      .state('about', {
        url: '/about',
        template: '<h1>Attempting to have this point at /main/about.html but this is for testing purposes</h1>'
      });

    $locationProvider.html5Mode(true);
  });

现在,测试一下,localhost:9000/about 会加载,但是当我尝试将状态链接到导航栏时,它会中断。

'use strict';

angular.module('sr388App')
  .controller('NavbarCtrl', function ($scope, $location) {
    $scope.menu = [{
      'title': 'Home',
      'link': '/',
      'title': 'About',
      'link': '/about'
    }];

    $scope.isCollapsed = true;

    $scope.isActive = function(route) {
      return route === $location.path();
    };
  });

控制台错误是 Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode

这里是导航栏 html

<div class="navbar navbar-default navbar-static-top" ng-controller="NavbarCtrl">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" type="button" ng-click="isCollapsed = !isCollapsed">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="/" class="navbar-brand">sr388</a>
    </div>
    <div collapse="isCollapsed" class="navbar-collapse collapse" id="navbar-main">
      <ul class="nav navbar-nav">
        <li ng-repeat="item in menu" ng-class="{active: isActive(item.link)}">
            <a ng-href="{{item.link}}">{{item.title}}</a>
        </li>
      </ul>
    </div>
  </div>
</div>

我确信我对正在发生的事情(应该正在发生的事情)的基本理解是有缺陷的。

我确实注意到在尝试加载状态而不更改 URL 时没有添加 #。

&lt;a ng-href="{{item.link}}"&gt;{{item.title}}&lt;/a&gt;

我以为我找到了问题,这会解决它

&lt;a ui-sref="{{item.link}}"&gt;{{item.title}}&lt;/a&gt;

但没有骰子。

任何意见/建议将不胜感激。我真正想做的就是基于此创建一个简单的 SPA 页面

http://i.imgur.com/sZYe15o.png

这只是简单地通过几个不同的视图状态。

/#关于 /#contact 等

【问题讨论】:

    标签: angularjs angular-ui-router angularjs-service


    【解决方案1】:

    在 ui-sref 中只需输入状态名称而不是 URL/链接

    ui-sref="about"
    

    然后就可以了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-29
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多