panagulis72 panagulis72 - 6 months ago 63
AngularJS Question

$state.go is not a function

I'm doing a web app with AngularJs and I'm using $state to travel into the app. After the login, I want to go to the home (state 'app.dashboard.home' of the absolute state 'app.dashboard).

In this page, there is a button to go in another view called expense (state 'app.dashboard.expense' of the absolute state 'app.dashboard). But when I click on the button, console display this error:


$state.go is not a function


Why?

This is the routing:

angular.module('app')
.run(
['$rootScope', '$state', '$stateParams',
function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}
]
)
.config(
['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$urlRouterProvider
.otherwise('/app/authenticating/');

$stateProvider
.state('app', {
abstract: true,
url: '/app',
templateUrl: 'common/mainApp.html'
})

.state('app.auth', {
url: '/authenticating/',
templateUrl: 'common/authenticating/authenticating.html',
controller: 'authenticatingCtrl',
resolve: {
deps: ['$ocLazyLoad',
function($ocLazyLoad) {
return $ocLazyLoad.load({
files: [
'common/authenticating/controllers/authenticatingCtrl.js'
]
}).then(function success(args) {
console.log('success');
return args;
}, function error(err) {
console.log(err);
return err;
});
}]
}
})

.state('app.dashboard', {
abstract: true,
url: '/dashboard',
templateUrl: 'dashboard/dashboard.html',
resolve: {
deps: ['$ocLazyLoad',
function($ocLazyLoad) {
return $ocLazyLoad.load({
files: [
'dashboard/dashboardCtrl.js'
]
}).then(function success(args) {
console.log('success');
return args;
}, function error(err) {
console.log(err);
return err;
});
}]
}
})

.state('app.dashboard.home', {
url: '/home',
views: {
'content': {templateUrl: 'dashboard/home/home.html'}
},
resolve: {
deps: ['$ocLazyLoad',
function($ocLazyLoad) {
return $ocLazyLoad.load({
files: [
'dashboard/home/homeCtrl.js'
]
}).then(function success(args) {
console.log('success');
return args;
}, function error(err) {
console.log(err);
return err;
});
}]
}
})

.state('app.dashboard.expense', {
url: '/newExpenseList',
views: {
'content': {templateUrl: 'dashboard/expense/new/new.html'}
},
resolve: {
deps: ['$ocLazyLoad',
function($ocLazyLoad) {
return $ocLazyLoad.load({
files: [
'dashboard/expense/new/newCtrl.js'
]
}).then(function success(args) {
console.log('success');
return args;
}, function error(err) {
console.log(err);
return err;
});
}]
}
})

}])


This is the dashboard.html:

<div class="content-wrap" nav-collapse-toggler type="swipe">
<main role="main" >
<div class="section-info">
<div class="section-subtitle">{{app.name}}</div>
</div>
<div id="content" class="content view-animate fade-up" ui-view="content"></div>
</main>
</div>


This is the homeCtrl:

'use strict';

angular.module('app').controller('HomeCtrl', ['$http', '$scope', '$rootScope', 'toaster', 'GlobalApplicationData', '$log', '$state', '$stateParams',
function($http, $scope, $rootScope, GlobalApplicationData, $log, $state, $stateParams) {
$scope.goToNewExpenseList = function() {
$state.go('app.dashboard.expense');
};
}]);


This is the home.html:

<div class="container" ng-controller="HomeCtrl">
<div class="form">
<button class="lol" ng-click="goToNewExpenseList()">Go to expense list</button>
</div>
</div>

Answer

The problem is in a wrong parameter array definition, and declared function parameters.

The forth is declared 'toaster', but that is missing in function params... so all others are shifted and contain different stuff

['$http', '$scope', '$rootScope', 'toaster', 'GlobalApplicationData', '$log', '$state', '$stateParams',
// original is missing 4th param
//function($http, $scope, $rootScope, GlobalApplicationData, $log, $state, $stateParams) 
// here we get $state as $state, because the 4th param will be toaster
function($http, $scope, $rootScope, toaster, GlobalApplicationData, $log, $state,  $stateParams)
Comments