M
M
Max2015-07-26 21:00:12
Angular
Max, 2015-07-26 21:00:12

How to solve a problem in Angular?

Hello, the admin deleted the previous question, so I will specify it.
33cacee7405c4c89b67f7a6fc28ab319.PNG
There is such a code

var app = angular.module('flapperNews',['ui.router'])
app.config([ 
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {

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

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

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

app.controller('MainCtrl', ['$scope','posts', function($scope, posts){
  $scope.posts = posts.posts;

  $scope.addPost = function(){
    if (!$scope.title || $scope.title === '')  {
      return; 
    }
    $scope.posts.push({'title': $scope.title, 'upvotes': 0, 'link': $scope.link,
        'comments': [
          {author: 'Joe', title: '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){
  $scope.post = posts.posts[$stateParams.id];
  $scope.addComment = function(){
  	if($scope.body === '') { return; }
    $scope.post.comments.push({
      body: $scope.body,
      author: 'user',
      upvotes: 0
    });
    $scope.body = '';
  };
}]);

I can't ask a specific question because I don't know what exactly the problem is.
If you believe the error in the console, then there is something wrong with the postsProvider, but what exactly I can’t figure out for the 3rd day. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Валентин Дубровский, 2015-07-26
@maxprof

$posts - у вас не такого провайдера, а есть posts

S
sim3x, 2015-07-26
@sim3x

Кликни на первую строку в консоли - тебе откроеся ман по твоей ошибке

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question