0

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

2
  • Do you want $scope.validElements to be bound to someService.someElement? Commented Mar 19, 2015 at 4:38
  • @WilliamGaul : Yes, i mean the condition is based on some element present in someService Commented Mar 19, 2015 at 4:40

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I will try to implement with this & will let you know. Thanks for the prompt reply. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.