Error injecting $ state into the controller

0

I'm having a problem injecting $state into my controller .

angular.module('myApp', ['ngMaterial'])
.controller('LoginController', function($scope, $rootScope,$state){

    $scope.userlogin = "";
    $scope.userkey = "";
    $scope.access = function(){
        console.log("depois")
        //$state.go('home');
    }

});

The following error is generated (Only when I include $state ):

  

Error: [$ injector: unpr]

Route file:

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

routerApp.config(function($stateProvider, $urlRouterProvider,$state) {

    $urlRouterProvider.otherwise('/login');

    $stateProvider

        .state('login', {
            url: '/login',
            templateUrl: 'index.html',
            controller: 'HomeController'
        })


        .state('index', {
            url: '/index',
            templateUrl: 'index.html',
            controller: 'HomeController'
        });

})
Good afternoon. I made the adjustments but it persists,

Route file:

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

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/login');

    $stateProvider

        .state('login', {
            url: '/login',
            templateUrl: 'login.html',
            controller: 'LoginController'
        })


        .state('index', {
            url: '/index',
            templateUrl: 'index.html',
            controller: 'HomeController'
        });

})

Controller:

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


app.controller('HomeController', function($scope,$rootScope,$state){
    console.log('HomeController');

    var nome = "teste";
    var dateAtual = new Date();
    var mensagens = "teste";

    $scope.msg  = mensagens;
    $scope.time = dateAtual;
    $scope.nome = nome;

    console.log(nome);
    console.log(dateAtual);
    console.log(mensagens);
})

// - Este controller sem o $state funciona corretamente.

app.controller('LoginController', function($scope,$rootScope){
    console.log('LOGINCONTROLLER');
});
  

angular.min.js: 118 Error: [$ injector: unpr]    link $ injector / unpr? p0 =% 24stateProvider% 20% 3C-% 20% 24state% 20% 3C-% 20HomeController

    
asked by anonymous 23.10.2016 / 05:45

1 answer

0

The problem is that you are injecting $state into the configuration file, which is not allowed. Remove it by leaving your line like this:

routerApp.config(function($stateProvider, $urlRouterProvider) {

And the problem must be solved.

Edited:

Based on your issue, the problem is that you are setting the modules in the wrong way.

Just to clarify, for you to create a module, you should use the following code:

angular.module('meuModulo', []);

If you want to use the same module for several controllers , you should always use the same module, but without [] . So:

angular.module('meuModulo').config(...
angular.module('meuModulo').controller(...

That's what you're missing, because you're creating 1 module for configuration and another module for controller .

var routerApp = angular.module('myApp', []);
var app = angular.module('myApp', []);

What you can do is like this:

var app = angular.module('myApp', []); //Cria somente 1 módulo

app.config(function($stateProvider, $urlRouterProvider) {...
app.controller('LoginController',...

Another important point I've noticed now and I believe you're not doing it.

In order to use the route system through .state you need to install the uiRouter module before. So your final code should look like this:

var app = angular.module('myApp', ['ui.router']); //Cria somente 1 módulo

app.config(function($stateProvider, $urlRouterProvider) {...
app.controller('LoginController',...

<script src="angular-ui-router/release/angular-ui-router.js"></script> //Altere o src para o caminho onde você salvar o script do uiRouter
    
23.10.2016 / 16:36