如何在select选项而不是object上获取字符串值:1等

时间:2017-08-10 16:40:22

标签: javascript html angularjs

如何使用实际字符串创建角度填充值。 我想得到这个

<option label="New York City" value="New York City">New York City</option>

而不是这个

<option label="New York City" value="object:3">New York City</option>

我的代码:

<div ng-controller="CountryCntrl">
    <div>
        City: <br>
        <select id="country" ng-model="states" ng-options="country for (country, states) in countries" required>
        <option value=''>Select</option>
        </select>
    </div>
    <br>

    <div>
        SubArea:<br>
        <select id="state" ng-disabled="!states" ng-model="cities" ng-options="state for (state,city) in states" required>
        <option value=''>Select</option></select>
    </div>
    <br>

    <div>
        SpecLoc:<br>
        <select id="city" ng-disabled="!cities || !states" ng-model="city">
        <option value=''>Select</option>
        <option ng-repeat="city in cities" value='{{city}}'>{{city}}</option></select>        
    </div>
</div>  

这是范围数组。请忽略错误的命名(国家/城市)。这是一个例子

    $scope.countries = {
                    'New York City': {
                        'manhattan': ['Battery Park','Chelsea','Chinatown / Lit Italy','Downtown','East Harlem','East Village','Financial District','Flatiron','Gramercy','Greenwich Village','Harlem / Morningside','Inwood / Wash Hts','Lower East Side','Midtown','Midtown East','Midtown West','Murray Hill','Nolita / Bowery','SoHo','TriBeCa','Union Square','Upper East Side','Upper West Side','West Village'],
                        'brooklyn': [],
                        'queens': [],
                        'bronx': [],
                        'staten island': [],
                        'new jersey': [],
                        'long island': [],
                        'westchester': [],
                        'fairfield co': [],
                        'fairfield co, CT': []
                    },
                    'Chicago': {
                        'city of chicago': [],
                        'north chicagoland': [],
                        'west chicagoland': [],
                        'south chicagoland': [],
                        'northwest indiana': [],
                        'northwest suburbs': []
                    },
                    'Washington': {
                        'district of columbia': [],
                        'northern virginia': [],
                        'maryland': []

                    }
                };
  }
]);

2 个答案:

答案 0 :(得分:0)

我假设您的countries对象是Json,并且它有一个value字段,您可以在其中保存citie的名称。说,您可以使用此<option value="{{country.value}}"></option>

设置选项的值
<div>
    City: <br>
    <select id="country" ng-model="states" ng-options="country for (country, states) in countries" required>
        <option value='{{country.value}}'>Select</option>
    </select>
</div>

答案 1 :(得分:0)

在我做了很多阅读之后,我找到了解决方案。 它非常简单,如果您希望您的选项值反映值而不是您刚添加的对象track by

<select id="country" ng-model="states" ng-options="country for (country, states) in countries track by country" required>
<option value=''>Select</option>
</select>

你会得到这个

<option label="New York City" value="New York City">New York City</option>

而不是这个

<option label="New York City" value="object:3">New York City</option>