Angularjs $stateChangeStart 检查字符串是否在对象中
Angularjs $stateChangeStart check if string is in object
我的 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
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路由器的原生$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
}
}
第二种方法需要ui更多的逻辑,但在我看来,它更清晰。
我的 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
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路由器的原生$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
}
}
第二种方法需要ui更多的逻辑,但在我看来,它更清晰。