Reputation: 1
I'm new to AngularJs and am stuck on a tutorial in the Routing section: https://thinkster.io/tutorials/mean-stack/routing-views-with-angular
Error Message: Uncaught Error: [$injector:modulerr] Failed to instantiate module flapperNews due to: Error: [$injector:nomod] Module 'flapperNews' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Prior to this error message I had a later version of angularjs (1.5.7) and it gave me this error - someone suggested online that you change the version. I had this error then: Uncaught Error: [$injector:modulerr]
Please help me so that I can move on and get back to learning.
Here are my files: index.html:
<html>
<head>
<title>My Angular App!</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
<script src="app.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>
<!-- rest of template -->
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span ng-click="incrementUpvotes(post)">▲</span>
<span ng-click="decrementUpvotes(post)">▼</span>
<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>
- upvotes: {{post.upvotes}}
</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>
</script>
</body>
</html>
app.js:
var count = 0;
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
count++;
return Math.floor(Math.random() * (max - min)) + min;
}
(function (){
angular.module('flapperNews', ['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl'
});
$urlRouterProvider.otherwise('home');
}
])
.factory('posts', [function(){
// service body
var obj = {
posts: []
};
return obj;
}])
.controller('MainCtrl',[
'$scope',
'posts',
function($scope, posts){
$scope.test = 'Reddit Emulator';
$scope.posts = posts.posts
$scope.addPost = function(){
if ($scope.title === '' || $scope.title == null)
$scope.title = 'post ' + (count + 1);
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: getRandomInt(0,25)
});
$scope.posts = posts.posts
$scope.title = '';
$scope.link = '';
};
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
$scope.decrementUpvotes = function(post) {
post.upvotes -= 1;
};
}]);
})
THanks!
Top of my files seem to have gotten cut off but I don't have time to fix it ATM. Sorry
Upvotes: 0
Views: 71
Reputation: 163
Your app.js is malformed.
Try this
angular.module('flapperNews', ['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl'
});
$urlRouterProvider.otherwise('home');
}
])
.factory('posts', [function(){
// service body
var obj = {
posts: []
};
return obj;
}])
.controller('MainCtrl',[
'$scope',
'posts',
function($scope, posts){
$scope.test = 'Reddit Emulator';
$scope.posts = posts.posts
$scope.addPost = function(){
if ($scope.title === '' || $scope.title == null)
$scope.title = 'post ' + (count + 1);
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: getRandomInt(0,25)
});
$scope.posts = posts.posts
$scope.title = '';
$scope.link = '';
};
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
$scope.decrementUpvotes = function(post) {
post.upvotes -= 1;
};
}]);
Then put the getRandomInt function inside the controller.
Upvotes: 0
Reputation: 676
Your Javascript code encapsulated between
(function (){
...
})
needs to be executed in order to load the code inside. For that, You can add (); at the end
(function (){
...
})();
Upvotes: 1