Search code examples
javascriptangularjsangularjs-ng-route

The controller with the name '..' is not registered


Im using AngularJS 1.6 and have a problem with routing, where Controller is unregistered, although it seemed that there was no problem. Tried different solutions, but they did not help.

products.html

<div ng-controller="ProductController">
    <div class="container">
         <div class="row">
            <div class="item col-sm-6 col-md-4 col-lg-3" ng-repeat="product in products">
                <h3>{{product.title}}</h3>
                <img class="product-photo" src="{{product.photoURL}}" alt="">
                <p>{{product.price}}</p>
                <button class="btn btn-cart" (click)="onAddToCart()">Add to cart</button>
            </div> 
        </div>
     </div>
</div>

Controller

ProductController.js

 var testApp = angular.module('testApp');
 testApp.controller('ProductController',function ProductController($scope,$http) {
    $scope.products = [
       { id:1, title: 'Margarita', photoURL:'...',price:'260 UAH'},
       { id:2, title: 'Margarita', photoURL:'...', price:'240 UAH'},
       { id:3, title: 'Margarita', photoURL:'...', price:'200 UAH'},
       { id:4, title: 'Margarita', photoURL:'...', price:'190 UAH'}
    ]; 
});

Routing

app-routing.js

var questApp = angular.module('testApp', ["ngRoute"])
    .config(function($routeProvider){
        $routeProvider.when('/products',
        {
            templateUrl:'./views/products.html',
            controller:'ProductController'
        });
        $routeProvider.when('/cart',
        {
            templateUrl:'./views/cart.html',
            controller:'CartController'
        });
        $routeProvider.otherwise({redirectTo: '/products'});
});

index.html

<!doctype html>
<html ng-app="testApp">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline|Cabin+Sketch|Fredericka+the+Great|Love+Ya+Like+A+Sister|Rancho|Roboto" rel="stylesheet">
</head>
<body>
<nav><a href="#!/products">Products</a>|<a href="#!/cart">Cart</a></nav>
<ng-view></ng-view>
<script src="https://code.angularjs.org/1.6.0/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.0/angular-route.min.js"></script>
<script src="./js/ProductController.js"></script>
<script src="./js/CartController.js"></script>
<script src="./js/app-routing.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>

Solution

  • load your app-routing.js ahead of the other two controllers

    <script src="./js/app-routing.js"></script>
    <script src="./js/ProductController.js"></script>
    <script src="./js/CartController.js"></script>
    

    and you don't have to declared the module again inside the controllers,

     var testApp = angular.module('testApp'); //remove this line
     testApp.controller('ProductController',function ProductController($scope,$http) {
        $scope.products = [
           { id:1, title: 'Margarita', photoURL:'...',price:'260 UAH'},
           { id:2, title: 'Margarita', photoURL:'...', price:'240 UAH'},
           { id:3, title: 'Margarita', photoURL:'...', price:'200 UAH'},
           { id:4, title: 'Margarita', photoURL:'...', price:'190 UAH'}
        ]; 
    });