AngularJS - dependent dropdowns: storing one value in model, using other as source of the next dropdown

前端 未结 1 621
忘掉有多难
忘掉有多难 2021-02-10 08:49

I have two dependent dropdowns. One shows countries another states. I would like the first one to save just the country id but use the entire object as source for the second dr

1条回答
  •  梦如初夏
    2021-02-10 09:46

    To keep things simple you can restructure your model as below where the keys act as ids:

    $scope.countries = {
        "c1" : {
            label : "Country 1",        
            states:{
                "s1":{
                    label:"State 1"             
                },
                "s2" : {
                    label:"State 2"             
                }
            }
        },
        "c2" : {
            label:"Country 2",      
            states:{
                "s3":{
                    label:"State 3"             
                },
                "s4" : {
                    label:"State 4"             
                }
            }
        }
    };
    

    HTML

    
    
    
    

    If by duplicate temporary variables(countrySource), you mean to use the same models for other dropdowns, selecting a country would change the options of all the country and state dropdowns on the page.

    0 讨论(0)
提交回复
热议问题