ClarkySmiverz77 ClarkySmiverz77 - 4 months ago 30
AngularJS Question

Issues with AngularJS Controller and module

I have several problems in the console and i'm unsure on how to fix.

The first error I am getting is

angular.js:68 Uncaught Error: [$injector:nomod] Module 'reviewsApp' 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.

along with ...

angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module reviewsApp due to:
Error: [$injector:nomod] Module 'reviewsApp' 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.


Can someone advise what I am doing wrong???

I have a simple controller like the following with the html at the bottom (Please not these files are seperate in my project)...

(function() {
'use strict';

angular
.module('reviewsApp')
.controller('ReviewsController', ReviewsController);

ReviewsController.$inject = ['$http'];

function ReviewsController ($http){
var vm = this;

var onReviewComplete = function(response) {
vm.reviews = response.data;
};

var onError = function(reason) {
vm.error = reason.data;
};

$http.get('../DataService/services/reviews/getReviews.php')
.then(onReviewComplete, onError);

//Insert Review
vm.insertReview = function(review){

//Create review object.
var reviewObj = {
'name' : review.name,
'message' : review.message
};

var insertRequest = $http.post('xxnx.com', reviewObj);

insertRequest.success(function(data, status, headers, config) {
vm.showSuccessAlert = true;
vm.clearForm(review)
});

insertRequest.error(function(data, status, headers, config) {
vm.showFailAlert = true;
});
};

vm.clearForm = function (review){
review.name = "";
review.message = "";
};
}

})();

<div class='container-fluid' ng-app='reviewsApp' ng-controller='ReviewsController'>

<div class='container'>

<div class='row'>
<h1 class='col-md-12 review-main-heading'> Reviews </h1>
</div>

<div class='row'>
<div class='col-md-9'>

Answer

You need to add the [] to mention the dependencies too,

Change

From

angular.module('reviewsApp')
    .controller('ReviewsController', ReviewsController);

To

angular.module('reviewsApp',[])
    .controller('ReviewsController', ReviewsController);