Vincent Ducroquet Vincent Ducroquet -4 years ago 98
Javascript Question

Is it possible to know if a ui.router state is already declared in AngularJS?

Here is my $stateProvider config

app.config(function ($stateProvider, $urlRouterProvider) {
var helloState = {
name: 'uploadForm',
url: '/uploadForm',
templateUrl: 'templates/step_Form/form.html?v=' + new Date().getDay()
}

var uploadState = {
name: 'uploadForm.upload',
url: '/upload',
templateUrl: 'templates/step_Form/form-upload.html?v=' + new Date().getDay()
}


var contentState = {
name: 'uploadForm.content',
url: '/content',
templateUrl: 'templates/step_Form/form-content.html?v=' + new Date().getDay()
}

$stateProvider.state(helloState);
$stateProvider.state(uploadState);
$stateProvider.state(contentState);

})


Is it possible to testif state is already declared?

Like this :

if($stateProvider.state.??? == null)
$stateProvider.state(helloState);


Because i have another page with a redirection, and it passes twice in the config and make an error 'State helloState is already declared'

Answer Source

You can use $state.get(), which returns an array of all declared states within your $stateProvider.

Of course, you'd have to inject $state in the function where you want to perform the check.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download