我的 app.run()
中有以下代码:
window.audienceConfig = InstitutionConfiguration.checkConfiguration(INSTITUTION_URL).then( response => {
window.audienceConfig = response.institution.landing_configuration;
console.log(window.audienceConfig);
});
$rootScope.$on('$stateChangeStart', function(event, toState) {...}
在我的 $rootScope.$on('$stateChangeStart')
中,我想检查我的 toState.name
是否等于任何 窗口。 audience.Config.somePage.routerName
window.audienceConfig
的 console.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.
}
实现它的第二种方法是使用我认为属于ui-router 的 native $state,以防你使用那个库,或者任何你用于改变状态。
$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
}
}
第二种方法需要更多逻辑,但在我看来,它更简洁。
关于javascript - Angularjs $stateChangeStart 检查字符串是否在对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50297964/