Global variables do not update in view with AngularJS

1

I'm creating a test application with AngularJs using the ngRoute routing system, the created controllers are working, however, the global variables in them are not being updated in the view. The browser expresses an error:

  

Error: [ng: areq] Argument 'controllers / ContactsController.js' is not a function, got undefined

This same error appears when the other controller is requested. I found a link, Controller not a function, got undefined, while defining controllers globally , where it says in the second example of the answer where it is necessary to put the $controllerProvider.allowGlobals(); function even though it did not work.

I tested other ways found in other posts but none served me.

public / index.html

<!doctype html>
<html ng-app="contatooh">
<head>
    <meta name="viewport" content="width=device-width">
    <meta charset="UTF-8">
    <title>Contatooh</title>
    <link rel="stylesheet" href="vendor/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="vendor/bootstrap/dist/css/bootstrap-theme.css">
</head>
<body class="container">
    <div ng-view></div>

    <script src="vendor/angular/angular.js"></script>
    <script src="vendor/angular-route/angular-route.js"></script>
    <script src="js/main.js"></script>
    <script src="js/controllers/ContatosController.js"></script>
    <script src="js/controllers/contatoController.js"></script>
</body>
</html>

public / js / main.js

angular.module('contatooh', ['ngRoute'])
    .config(function($controllerProvider, $routeProvider) {

        $controllerProvider.allowGlobals();

        $routeProvider.when('/contatos',{
            templateUrl: '../partials/contatos.html',
            controller: 'controllers/ContatosController.js'
        })
        .when('/contato/:contatoId', {
            templateUrl: '../partials/contato.html',
            controller: 'controllers/ContatoController.js'
        })
        .otherwise({redirectTo: '/contatos'});

    });

public / controllers / ContactsController.js

angular.module('contatooh').controller('ContatosController',
    function($scope) {

        $scope.total = 0;
        $scope.incrementa = function() {
            $scope.total++;
        };

        $scope.contatos = [{
            "_id": 1,
            "nome": "Contato Angular 1",
            "email": "[email protected]"
        }, {
            "_id": 2,
            "nome": "Contato Angular 2",
            "email": "[email protected]"
        }, {
            "_id": 3,
            "nome": "Contato Angular 3",
            "email": "[email protected]"
        }];
    });

public / partials / contacts.html

<div class="jumbotron">
    <h1 class="text-center">
        Bem-vindo ao Contatooh
    </h1>
</div>

<button class="btn btn-primary" ng-click="incrementa()">
    Novo
</button>

<p>Contatos cadastrados: {{total}}</p>

<div class="table-responsive">
    <table class="table table-hover">
        <tr>
            <th>NOME</th>
            <th>E-MAIL</th>
            <th class="text-center">Ação</th>
        </tr>
        <tr ng-repeat="contato in contatos">
            <td>
                <a>{{contato.nome}}</a>
            </td>
            <td>{{contato.email}}</td>
            <td class="text-center">
                <button class="btn btn-warning">
                    Remover
                </buttom>
            </td>
        </tr>
    </table>
</div>

As I said earlier, views are being loaded and generated on the screen, only the variables in $scope are not being displayed.

    
asked by anonymous 24.05.2016 / 00:08

2 answers

2

I may be wrong, but I believe that the controller definition within an object consumed by when() should point to the declared function name of a controller and not to a .js file - even because the same javascript can set multiple controllers . So:

    $routeProvider.when('/contatos',{
        templateUrl: '../partials/contatos.html',
        controller: 'ContatosController'
    })
    
24.05.2016 / 14:34
1

Do it this way

 $routeProvider.when('/contatos',{
        templateUrl: '../partials/contatos.html',
        controller: 'ContatosController'
    })

You have to inform the controller and not the .js file

    
24.05.2016 / 00:20