Tushar Khanna
Tushar Khanna

Reputation: 438

How to make scoped variable in Controller based on some condition in Angularjs

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

Answers (1)

Aaron
Aaron

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

Related Questions