Answer the question
In order to leave comments, you need to log in
How to solve a problem in Angular?
Hello, the admin deleted the previous question, so I will specify it.
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 = '';
};
}]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question