javascript - 是否可以知道 ui.router 状态是否已在 AngularJS 中声明?

标签 javascript angularjs angular-ui-router

这是我的 $stateProvider 配置

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);

})

是否可以测试状态是否已声明?

像这样:

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

因为我有另一个带有重定向的页面,并且它在配置中传递了两次并出现错误“State helloState is已经声明”

最佳答案

您可以使用 $state.get(),它返回 $stateProvider 中所有已声明状态的数组。

当然,您必须在要执行检查的位置注入(inject) $state

关于javascript - 是否可以知道 ui.router 状态是否已在 AngularJS 中声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44065575/

相关文章:

Javascript 事件删除

javascript - Backbone.js 和嵌套集合

javascript - 从 AngularJS 获取数据到 Silverlight

javascript - 从装饰器设置 View 的名称 - Angular Ui Router

angularjs - Angular - UI 路由器 - 嵌套状态 - 浏览器后退按钮无法按预期工作

javascript - AngularJS、Rails 和 ui-router 不显示模板

javascript - 将 if/else 逻辑添加到 JavaScript 过滤器

javascript - 如何在 Vue.js 中将特定颜色传递给父组件?

javascript - 如何访问两者都需要: 'ngModel' and controller properties on directive

javascript - angular.fromJson(data.Body) 与 JSON.parse(data.Body)