ClarkySmiverz77
ClarkySmiverz77

Reputation: 203

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 separate 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'>

Upvotes: 0

Views: 256

Answers (2)

Teymur Mardali
Teymur Mardali

Reputation: 847

Try like this:

var reviewsApp = angular.module('reviewsApp',[]);

reviewsApp.controller('ReviewsController',['$scope', '$http' function($scope, $http) {

// Your own code which is in - "function ReviewsController(){}" - comes here

}]);

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

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);

Upvotes: 1

Related Questions