我对Angular很陌生,所以我很抱歉,如果这很简单。Angular指令国家/地区
我有一个工作应用程序,有多个地方,用户必须首先从国家列表中选择一个国家,然后从选择区域中选择一个国家。我已经有每个国家的国家名单和地区名单。如果系统没有选定国家的区域,则区域选择器将变为纯文本框。
它运行良好,但我有8个不同的地方,我需要这个功能。例如,该应用程序要求出生地和死亡地点。
看起来我应该把这个功能放到一个指令中,但是我发现的所有例子都是从模型中传入一个单独的字段(即ng-model ='country')或整个范围对象。我需要为模型的每个实例传递模型的特定国家和地区字段。
我该怎么做?
这是我现在拥有的一个例子。我怎样才能把它变成一个单一的“国家/地区选择器”指令,以便我可以重复使用它,而不是基本上复制并粘贴到所有地方。
<div class="form-group">
<label for="birth_country" class="col-sm-3 control-label">Birth Country</label>
<div class="col-sm-9">
<select id="birth_country" ng-model="person.birth_country" ng-options="country.text as country.text for country in country_options" class="form-control" ng-change="getBirthRegions(person.birth_country)" ></select>
</div>
</div>
<div class="form-group">
<label for="birth_region" class="col-sm-3 control-label">Birth State/Region</label>
<div class="col-sm-9">
<input id="birth_region" type="text" ng-model="person.birth_region" class="form-control" placeholder="Enter the birth state/region" ng-hide="birth_region_options.length > 0" />
<select id="birth_region" ng-model="person.birth_region" ng-options="region.text as region.text for region in birth_region_options" class="form-control" ng-show="birth_region_options.length > 0"></select>
</div>
</div>
<div class="form-group">
<label for="death_country" class="col-sm-3 control-label">Death Country</label>
<div class="col-sm-9">
<select id="death_country" ng-model="person.death_country" ng-options="country.text as country.text for country in country_options" class="form-control" ng-change="getDeathRegions(person.death_country)" ></select>
</div>
</div>
<div class="form-group">
<label for="death_region" class="col-sm-3 control-label">Death State/Region</label>
<div class="col-sm-9">
<input id="death_region" type="text" ng-model="person.death_region" class="form-control" placeholder="Enter the death state/region" ng-hide="death_region_options.length > 0" />
<select id="death_region" ng-model="person.death_region" ng-options="region.text as region.text for region in death_region_options" class="form-control" ng-show="death_region_options.length > 0"></select>
</div>
</div>