我的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
是否等于window.audience.Config.somePage.routerName
console.log()
window.audienceConfig
的{{1}}输出如下:
"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中执行此操作?
答案 0 :(得分:1)
我实际上知道两种方法:
第一个,我见过许多人不喜欢它,但它包括检查你所在州的州名是否存在。在$stateChangeStart
事件函数中添加它;您可以使用以下代码实现它。例如,如果您想检查是否要使用仪表板。
if (toState.name.indexOf('dashboard') > -1 ) {
//do something.
}
实现它的第二种方式是使用我认为属于 ui-router的本机$状态,以防你使用该库,或任何你用于改变状态。
$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
}
}
第二种方法需要更多的逻辑,但在我看来,它更清洁。