AngularJS选择选项后保留内容并重置为空白的选项

时间:2014-04-18 22:33:30

标签: javascript angularjs

这是当前的代码(我没有写它只是试图使用它) -

<select type="text" id="{{for}}" name="{{for}}" ng-model="model.value" class="form-control input-sm" placeholder="{{placeholder}}" ng-options="c.name as c.name for c in countries track by c.code">
  <option value="">— Select —</option>
</select>

和countries数组似乎设置如此

$http.get('services/dictionary/countries').then(function(response) {
   _.chain(response.data).map(function(country) {
     return {
       code: country.id,
       digraph: country.digraph,
       trigraph: country.trigraph,
       name: country.name
     };
   }).sortBy('name').each(function(country) {
      dictionary.countries.push(country);
   });

   deferred.resolve(dictionary.countries);
}, function(error) {
    $log.error('dictionary:', error);
});

我们从一些字典服务中提取国家/地区列表,并尝试将它们显示为选择选项。 (我们这里并没有真正使用有向图和三字图,但是对于使用相同字典调用的其他区域则需要它)。

问题1是,我们可以设置选项并保存,但是选择列表不会显示我保存的对象,特别是在我存储和刷新它之后。我认为这是因为我们没有正确设置值= “”我们设置了ng-options,但是在尝试多次迭代后我无法正常工作(不太了解文档)。

问题2是,在这个以及我们拥有的所有其他选择下拉列表中,我无法找到恢复为空选择的方法。我有一个value =“”默认选项但是在选中时它不会使ng模型空白。我们需要这个允许用户搞砸,'哎呀我甚至不打算设置那个领域。输入东西。

非常感谢帮助帮派。

1 个答案:

答案 0 :(得分:0)

问题1:您是否在刷新时将ngModel值设置为保存的值?只有这样才能在刷新时选择它。

问题2:将模型设置为控制器中的空对象: model = {};