Search code examples
angularjsangular-ui-routerangularjs-service

Angular-Fullstack + UI router isn't routing properly


I'm attempting to route my application using UIrouter with the current yeoman-fullstack generator without mongoDB. In this example of my issue, everything is default to test and make sure the UI-router is working properly.

Mainly because I'm not understanding and missing something silly and noobish here, so I apologize.

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

I'm attempting to edit my routes in client/app/app.js using

'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);
  });

Now, testing that, localhost:9000/about will load, but when I try to link the state to the navbar it breaks.

'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();
    };
  });

The console error is Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode

Here is the navbar 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>

I'm certain my fundamental understanding of what's happening (supposed to be happening) is flawed.

I did notice the # is not being added when attempting to load states without changing URL.

<a ng-href="{{item.link}}">{{item.title}}</a>

I thought I had found the problem, and this would fix it

<a ui-sref="{{item.link}}">{{item.title}}</a>

But no dice.

Any advice/suggestions would be greatly appreciated. All I'm really trying to do is to create a simple SPA page based on this

https://i.sstatic.net/pK7Mh.png

That would just simply run through a few different view states.

/#about /#contact etc.


Solution

  • In ui-sref just put the state name instead of the URL/link

    ui-sref="about"
    

    Then it will work