Reputation: 438
My code in Angularjs Controller is like :
$scope.validElements = [
{
"id": "One",
},
{
"id": "Two",
},
{
"id": "Three",
},
{
"id": "Four",
}
];
How can i make validElements to be different based on condition
( if(someService.someElement == "ABC") )
then
$scope.validElements = [
{
"id": "One",
},
{
"id": "Three",
}
];
otherwise
$scope.validElements = [
{
"id": "One",
},
{
"id": "Two",
},
{
"id": "Three",
},
{
"id": "Four",
}
];
Please give suggestions if it is possible or any rough idea how it can be done
Upvotes: 0
Views: 56
Reputation: 2450
It seems you want to create another scoped variable and set it equal to someService, and then place a watch on someSome element. You can then handle the condition in the watch function. I've done this to watch service variables
$scope.validElements = [ // set default here
{
"id": "One",
} // ...
];
$scope.someService = someService;
$scope.$watch('someService.someElement', function(newValue, oldValue) {
// or a switch statment
if(newValue == 'ABC'){
$scope.validElements = [
{
"id": "One",
},
{
"id": "Three",
}
];
}else{
// handle else condition
$scope.validElements = [
{
"id": "One",
},
{
"id": "Two",
},
{
"id": "Three",
},
{
"id": "Four",
}
];
}
});
Hope this helps! good luck
Upvotes: 1