Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm following this tutorial to get familiar with the MEAN stack. I got to the point where they start using a routing configuration using ui-router. I've set everything up like they have it, but my page isn't rendering anything in my <ui-view> element.

(this was working before I added the routing config)

Here's what my files look like:

index.html:

<!DOCTYPE html>
<html>
<head>
  <title>Flapper News</title>
  <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
  <script scr="indexHomeState.js"></script>
  <script src="indexController.js"></script>
  <script src="postsService.js"></script>
  <style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>
<body ng-app="flapperNews">

  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <ui-view></ui-view>
    </div>
  </div>


    <script type="text/ng-template" id='/home.html'>
      <div class="page-header">
        <h1>Flapper News</h1>
      </div>

      <div ng-repeat="post in indexCtrl.getPosts() | orderBy:'-upvotes'">
        <span class="glyphicon glyphicon-thumbs-up"
          ng-click="indexCtrl.incrementUpvotesForPost(post)"></span>
        {{post.upvotes}}
        <span style="font-size:20px; margin-left:10px;">
          <a ng-show="post.link" href="{{post.link}}">
            {{post.title}}
          </a>
          <span ng-hide="post.link">
            {{post.title}}
          </span>
        </span>
      </div>

      <form ng-submit="indexCtrl.addPost()"
        style="margin-top:30px;">
        <h3>Add a new post</h3>

        <div class="form-group">
          <input type="text"
            class="form-control"
            placeholder="Title"
            ng-model="indexCtrl.title"></input>
        </div>
        <div class="form-group">
          <input type="text"
          class="form-control"
          placeholder="Link"
          ng-model="indexCtrl.link"></input>
        </div>
        <button type="submit" class="btn btn-primary">Post</button>
      </form>
    </script>


</body>
</html>

routingConfig (indexHomeState.js)

angular.module('flapperNews')
.config('indexHomeState', indexHomeState);


indexHomeState.$inject = ['$stateProvider', '$urlRouterProvider'];

function indexHomeState($stateProvider, $urlRouterProvider) {

    $stateProvider
    .state('home', {
        url: '/home',
        templateUrl: '/home.html',
        controller: 'indexController as indexCtrl'
    });
    $urlRouterProvider.otherwise('home');


}

my angular app/controller declaration:

angular.module('flapperNews', ['ui.router'])
.controller('indexController', indexController);

indexController.$inject = ['postsService'];

function indexController(postsService) {
    var vm = this;
    vm.test = 'Hello world';

    vm.getPosts = postsService.getPosts;
    vm.addPost = postsService.addPost(vm.title, vm.link);
    vm.incrementUpvotesForPost = postsService.incrementUpvotesForPost;

}

post service

angular.module('flapperNews')
.factory('postsService', postsService);

function postsService() {
    var serv = this;
    var posts = [
      {title: 'post 1', upvotes: 5},
      {title: 'post 2', upvotes: 2},
      {title: 'post 3', upvotes: 15},
      {title: 'post 4', upvotes: 9},
      {title: 'post 5', upvotes: 4}
    ];
    function getPosts() {
        return posts;
    }
    function addPost(title, link) {
        if(!title || title === '') { return; }
        posts.push({
            title: vm.title,
            link: vm.link,
            upvotes: 0
        });
        vm.title = '';
        vm.link = '';
    }
    function incrementUpvotesForPost(post) {
        post.upvotes ++;
    }
    return {
        getPosts: getPosts,
        addPost: addPost,
        incrementUpvotesForPost: incrementUpvotesForPost
    }

}
share|improve this question
    
Any console errors? have you added postsService to your angular.module('flapperNews') ? –  Pankaj Parkar Aug 18 at 17:22
    
@PankajParkar yea I'll post the rest of the code –  Abdul Ahmad Aug 18 at 17:22
    
please do post error too.so that would help to find the direction to solve it.. –  Pankaj Parkar Aug 18 at 17:23
    
@PankajParkar theres no error. There's just nothing displaying –  Abdul Ahmad Aug 18 at 19:58

1 Answer 1

up vote 2 down vote accepted

Your code looks good only the thing you are missing is, you need to change

$urlRouterProvider.otherwise('home');

to

$urlRouterProvider.otherwise('/home');

Also you have misspelled the src to scr for indexHomeState.js, That restricting your JS files from loading

Scripts

<script src="indexController.js"></script>

As you don't have indexHomeState provider in your app, you shouldn't add indexHomeState as a string inside config block.

angular.module('flapperNews')
  .config('indexHomeState', indexHomeState);

It would be simply

angular.module('flapperNews')
  .config(indexHomeState); //removed `'indexHomeState',` from config

Demo Plunkr

share|improve this answer
    
I just tried it but it didn't work –  Abdul Ahmad Aug 18 at 17:21
    
@AbdulAhmad do look at edit.. –  Pankaj Parkar Aug 18 at 20:47
    
let me try the source thing –  Abdul Ahmad Aug 18 at 20:55
    
@AbdulAhmad also updated answer with one more mistake with config block..and do look at plunkr for further query –  Pankaj Parkar Aug 18 at 20:58
    
thank you for the plunkr, I'm looking at it now and comparing with my code!! –  Abdul Ahmad Aug 18 at 21:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.