How can I access a variable in my controller that is on the same file as my .config? I'm trying to authenticate if the user is currently login before loading a view like this:
'use strict';
// Declare app level module which depends on views, and components
angular.module('myApp', [
'ngRoute',
'ngStorage',
'ui.router',
'myApp.userView',
'myApp.usuarios'
])
.controller("MainCtrl",['$scope','$localStorage',function($scope,$localStorage) {
$scope.user = {}
$scope.save = function(u) {
$localStorage.user = u;
};
$scope.load = function() {
$scope.user = $localStorage.user;
console.log($scope.user);
};
$scope.logoff= function() {
$localStorage.$reset();
$scope.load();
};
$scope.loginPage= function() {
var path = "/login/";
window.location.href = path;
};
$scope.load();
}])
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
// $urlRouterProvider.otherwise('/404');
$stateProvider
.state('userView', {
url: '/users_config',
templateUrl: 'resources/userView/userView.html',
controller: 'userViewCtrl',
resolve: {
security: ['$q', function($q){
if($scope.user == null){
$state.go("401");
}
}]
}
})
.state('404', {
url: '{path:.*}',
templateUrl: 'resources/errorView/404.html'
})
.state('401', {
url: '{path:.*}',
templateUrl: 'resources/errorView/401.html'
});
});
I want to access $scope.user in the userView state to decide wheneaver it should be loaded or not. Any pointer would be appreciated.