Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs$stateChangeStart检查字符串是否在对象中_Javascript_Angularjs_Angular Ui Router - Fatal编程技术网

Javascript Angularjs$stateChangeStart检查字符串是否在对象中

Javascript Angularjs$stateChangeStart检查字符串是否在对象中,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我的app.run()中有以下代码: 在我的$rootScope.$on(“$stateChangeStart”)中,我想检查我的toState.name是否等于window.audition.Config.somePage.routerName的控制台.log()输出以下内容: "dashboard": { "routerName": "basicLayout.dashboard", "audience": [ "faculty", "stude

我的
app.run()
中有以下代码:

在我的
$rootScope.$on(“$stateChangeStart”)
中,我想检查我的
toState.name
是否等于
window.audition.Config.somePage.routerName
控制台.log()
输出以下内容:

"dashboard": {
    "routerName": "basicLayout.dashboard",
    "audience": [
        "faculty",
        "student",
        "employee"
    ]
},
"profile": {
    "routerName": "basicLayout.content",
    "audience": [
        "faculty",
        "student"
    ]
},
"academic_resources": {
    "routerName": "basicLayout.academicResources",
    "audience": [
        "student"
    ]
},
"university_services": {
    "routerName": "basicLayout.universityServices",
    "audience": [
        "student"
    ]
},
"support_center": {
    "routerName": "basicLayout.supportCenter",
    "audience": [
        "student",
        "faculty",
        "employee"
    ]
},
"progress_coach": {
    "routerName": "basicLayout.progressCoach",
    "audience": [
        "student"
    ]
}

我需要验证每次更改路由时,
toState.name
是否等于上述对象的任何
routerName
属性,是否有一种方法可以在if中执行此操作?

我实际上知道两种方法:

第一个,我见过许多人不喜欢它,但它包括检查你所在州的名称是否存在于你所在州。将其添加到
$stateChangeStart
事件函数中;您可以通过以下代码实现它。例如,如果要检查是否要进入仪表板

if (toState.name.indexOf('dashboard') > -1  ) {
            //do something. 
        }
实现它的第二种方法是使用原生$state,我认为它属于ui路由器,以防您使用该库或任何用于更改状态的库

$rootScope.$on('$stateChangeStart', function(event, toState) {...
  var stateName = $state.name; 
  console.log(stateName)
  console.log(toState)
 //Check your console because stateName and toState are formated as an 
  //object or smth like that.
 //You need to read and extract the first element of the array with a 
 //foreach. After that, you may use the real name from the state and compare
if (stateNameExtracted  === toStateExtracted){
     //do Something
    }
}
第二种方法需要更多的逻辑,但在我看来,它更干净

$rootScope.$on('$stateChangeStart', function(event, toState) {...
  var stateName = $state.name; 
  console.log(stateName)
  console.log(toState)
 //Check your console because stateName and toState are formated as an 
  //object or smth like that.
 //You need to read and extract the first element of the array with a 
 //foreach. After that, you may use the real name from the state and compare
if (stateNameExtracted  === toStateExtracted){
     //do Something
    }
}