user2993456
user2993456

Reputation:

fake comments wont appear the posts object:

-Please see my EDIT at the bottom of my OP -Please see my plunker here

I'm learning AngularJS by going through a walkthrough located here.

I am on the step titled 'Faking comment data'. I have built out several models and a service within my float.js file, but something must be wrong since I can not see any posts containing the fake data which I just added. What needs to be fixed in order for me to see this fake data as per my tutorial?

Here is float.js:

angular.module('FLOAT', ['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: '/home.html',
      controller: 'MainCtrl'
    });

    $stateProvider
    .state('posts', {
      url: '/posts/{id}',
      templateUrl: '/posts.html',
      controller: 'PostsCtrl'
    });

  $urlRouterProvider.otherwise('home');
}])

angular.module('FLOAT', [])
.factory('posts', [function(){
  var o = {
    posts: []
  };
  return o;
}])

angular.module('FLOAT', [])
  .controller('MainCtrl', [
'$scope',
'posts',
function($scope, posts){
        $scope.test = 'Hello world!';


      // We are inside a controller here:
        $scope.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
      }];

        $scope.posts = posts.posts;

        $scope.addPost = function(){
          if(!$scope.title || $scope.title === '') { return; }
          $scope.posts.push({
              title: $scope.title,
              link: $scope.link,
              upvotes: 0,
              comments: [
                {author: 'Joe', body: 'Cool post!', upvotes: 0},
                {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
              ]
            });
          $scope.title = '';
          $scope.link = '';
        };

        $scope.incrementUpvotes = function(post) {
          post.upvotes += 1;
        };

    }

  ]);

  angular.module('FLOAT', [])
  .controller('PostsCtrl', [
'$scope',
'$stateParams',
'posts',
function($scope, $stateParams, posts){

}]);

here is my index.html file:

<html>
  <head>
    <title>FLOAT</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
    <script src="float.js"></script>
    <style> .glyphicon-thumbs-up { cursor:pointer } </style>
  </head>

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

  <!-- rest of template -->
  </script>

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

      <div class="page-header">
        <h1>FLOAT</h1>
      </div>

      <div ng-repeat="post in posts | orderBy:'-upvotes'">
        <span class="glyphicon glyphicon-thumbs-up"
          ng-click="incrementUpvotes(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="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="title"></input>
        </div>
        <div class="form-group">
          <input type="text"
          class="form-control"
          placeholder="Link"
          ng-model="link"></input>
        </div>
        <button type="submit" class="btn btn-primary">Post</button>
      </form>

    </div>
  </div>
</body>
</html>

EDIT:

OK, as per these two answers I have placed everything within one main module, and one ui.router modile; but now {{post.upvotes}} {{post.title}} {{post.title}} all bind incorrectly to my view. the code shows up instead of evaluating)

Here is the edited FLOAT.js file:

// ui-router module
angular.module('FLOAT', ['ui.router'])

.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: '/home.html',
      controller: 'MainCtrl'
    });

    $stateProvider
    .state('posts', {
      url: '/posts/{id}',
      templateUrl: '/posts.html',
      controller: 'PostsCtrl'
    });

  $urlRouterProvider.otherwise('home');
}]);

// main module
angular.module('FLOAT', [])

.factory('posts', [function(){
  var o = {
    posts: []
  };
  return o;
}])

.controller('MainCtrl', [
'$scope',
'posts',
function($scope, posts){
        $scope.test = 'Hello world!';


      // We are inside a controller here:
        // $scope.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
      // }];

        $scope.posts = posts.posts;

        $scope.addPost = function(){
          if(!$scope.title || $scope.title === '') { return; }
          $scope.posts.push({
              title: $scope.title,
              link: $scope.link,
              upvotes: 0,
              comments: [
                {author: 'Joe', body: 'Cool post!', upvotes: 0},
                {author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
              ]
            });
          $scope.title = '';
          $scope.link = '';
        };

        $scope.incrementUpvotes = function(post) {
          post.upvotes += 1;
        };
}])

.controller('PostsCtrl', [
'$scope',
'$stateParams',
'posts',
function($scope, $stateParams, posts){
}]);

Upvotes: 1

Views: 83

Answers (2)

JanisP
JanisP

Reputation: 1243

Edit: Here is working plnkr for you: http://plnkr.co/edit/2DJcgERl2j2JgoduU56H?p=preview There were several structural changes, but mostly everything was working. The biggest issue was that you were not using any controller. Ss I added ng-controller="MainCtrl"

This code is creating new model 3 times. You need to access it, instead of creating.

For creating:

angular.module('FLOAT', [])

For accessing, you can use:

angular.module('FLOAT').controller(..)

If you have everything in one file, you can just use

angular.module('FLOAT',[]) //create
.controller( .. ) //access
.factory( .. ); //access

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

You have several problems including re-declaring modules and overwriting data.

A module declaration ( only done once) includes the dependency array argument. After that a module reference getter does not include the second argument

In the controller you create an array of $scope.posts but then right after that you reassign $scope.posts = posts.posts;

this wipes out what you previously had in $scope.posts

A simple fix is move all the new data to the o.posts array in your posts factory and then only set $scope.posts = posts.posts;

Upvotes: 1

Related Questions