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
asked Mar 19, 2015 at 4:24
Tushar Khanna
4486 silver badges22 bronze badges
1 Answer 1
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
answered Mar 19, 2015 at 4:44
Aaron
2,4501 gold badge21 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Tushar Khanna
I will try to implement with this & will let you know. Thanks for the prompt reply. :)
lang-js
$scope.validElementsto be bound tosomeService.someElement?someService