I am quite new new to AngularJs. In my controller i have a $rootScope variables which i want to assign a value dynamically depending on conditions.
Following is my controller code:
controller(){
$rootScope.tab1;
$rootScope.tab2;
$rootScope.tab3;
$rootScope.tab3;
$rootScope.tab4;
$scope.changeVal("tab2");
$scope.changeVal=function(tabname){
vm.tabs=['tab1','tab2','tab3','tab4','tab5'];
angular.forEach(vm.tabs, function(val) {
if(tabname===val)
{
//here $rootScope.tab2 value should be changed
//something like bellow
$rootScope.{{tabname}}="myValue";
}
else{
$rootScope.$eval(val)="";
}
});
}
}
I know that , by applying multiple if or switch conditions this can be done. But, finding some optimized solution. Any help would be appriciated. Thanx in advance.
$rootScope? Is using a local$scopenot enough? Try limiting the digest cycle if possible. Another way to store application variables is to useservices(singleton).