【问题标题】:AnglularJS - ngOptions selected option with string value for the model, but objects as optionsAngularJS - ngOptions 为模型选择了带有字符串值的选项,但将对象作为选项
【发布时间】:2016-08-21 23:31:38
【问题描述】:

现在已经搜索了几个小时,试图了解这是如何工作的。

但实际上我已经得到了

<div class="form-group">
    <?php $field = 'member_address_country'; ?>
    <label for="<?=$field?>">Country</label>
    <select class="form-control" id="<?=$field?>" ng-options="country.country_id as country.country_name for country in countries track by country.country_id" ng-model="a.<?=$field?>">
        <option value="" disabled="disabled" selected="selected">-- Select --</option>
    </select>
</div>

我有一个国家数组,存储在$scope.countries 中,对象看起来类似于

[
    {country_id: "1", country_name: "england"},
    {country_id: "2", country_name: "america"},
    {country_id: "3", country_name: "other"},
]

我有一个地址数组,它们在标记为a 的 ngRepeat 中,所以a.member_address_country 是一个字符串,其中包含一个 int id。在这个例子中是"2"

因此,尽管我使用 ngOptions 和 track by country.country_id 并且模型的值是来自 country_id 的 id,正如预期的那样,当呈现 ngRepeat 时它只显示 -- Select -- 而不是从数组中选择对象,基于跟踪的 id。

我做错了什么,我该如何解决?

我已经添加了问题的代码笔 -> http://codepen.io/owenmelbz/pen/XdxrMO?editors=1010

这里还有另一个例子 ->

angular.module('app', [])
  .controller('testController', function($scope) {

    $scope.a = {
      member_address_country: 100
    }

    $scope.countries = [{
      country_id: 100,
      country_name: "england"
    }, {
      country_id: 200,
      country_name: "uk"
    }, {
      country_id: 300,
      country_name: "london"
    }]

  });
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.min.js"></script>

<div class="form-group" ng-app="app" ng-controller="testController">
  <label for="member_address_country">Country</label>
  <select class="form-control" id="member_address_country" ng-options="country.country_id as country.country_name for country in countries track by country.country_id" ng-model="a.member_address_country">
  </select>model value is: {{a.member_address_country | json}}
</div>

【问题讨论】:

  • 您说您的国家/地区数组存储在一个名为 $scope.country 的变量中,但在您的代码中,您正试图绑定到一个 countries 变量。哪个是正确的?
  • 抱歉打错了,它的$scope.countries你可以在codepen.io/owenmelbz/pen/XdxrMO?editors=1010看到它发生

标签: angularjs select ng-repeat ng-options


【解决方案1】:

HTML

<select ng-change="getName(a.member_address_country)" class="form-control" id="member_address_country" ng-options="country.country_id as country.country_name for country in countries" ng-model="a.member_address_country">
        </select> model value is: {{a.member_address_country| json}}
    Name after change = {{name}};

JS

    $scope.a = {
       country_id: 100,
          country_name: "england"
      }

  $scope.getName = function(countryid){
    angular.forEach($scope.countries,function(data){
      if(data.country_id == countryid){

        $scope.name = data.country_name;
      }

    });
  };

【讨论】:

  • 嗨 Mohsin,$scope.a 不是该国家/地区的模型,它包含一个完整的地址对象,因此将其更改为该模型会破坏其他所有内容,该属性称为 $scope.a.member_address_country - 这就是下拉菜单需要更新。
  • 试试这个新信息
  • 如果你看到这支笔 -> codepen.io/owenmelbz/pen/XdxrMO?editors=1010 你会看到默认情况下应该选择“england”项,尽管它不是,它是空白的
  • 不完全,它返回一个对象,具体问题是它需要返回标题中提到的country_id的'字符串值',这就是为什么使用track by country.country_id
【解决方案2】:

如果我理解正确,您想知道为什么您默认使用包含占位符文本的选项?

<option value="" disabled="disabled" selected="selected">-- Select --</option>

两件事:

  1. 您不需要包含该选项,除非您将它作为其他内容的挂钩。
  2. 如果您确实想使用它,但不希望它以默认方式显示(老实说,这对我来说没有多大意义),然后删除 selected="selected" 属性。李>

更新

在那种情况下,我看了一下你的例子。在上面的评论中问你问题的人是正确的。您的变量有两个不同的名称:国家和国家。选择其中一个并让它们匹配,应该很好。

见:

您正在引用此变量 country.country_id

但在你的范围内,你声明

$scope.countries = [
{
  country_id: 100,
  country_name: "england"
},
{
  country_id: 200,
  country_name: "uk"
},
{
  country_id: 300,
  country_name: "london"
}
]

更新 2

我认为这就是你想要的或接近的:

ng-options="country.country_id for country in countries track by id"

【讨论】:

  • 它只是一个占位符项目,最终将在 ng-if="editing_mode" 内,但即使您删除占位符项目,它也会默认为空白。我刚刚用它创建了一个codepen -> codepen.io/owenmelbz/pen/XdxrMO?editors=1010 你可以看到模型的默认值为100,但select没有使用它。
  • 如上一条评论中所述,这只是问题中的一个错字,如果您查看 codepen 上的真实代码示例,您将看到实际问题,除非我不明白如何track by 工作。
  • 所以如果我用countries.country_id 替换country.country_id 的所有实例应该可以解决它吗?正如我在 fork codepen.io/owenmelbz/pen/pyxzOx?editors=1010 上尝试过的那样,它给了我一个完全不同的国家,并在值中设置了 undefined
  • 我用那个替换了代码,现在它做错了两件事:(它将下拉列表的标签设置为一个值,而不是国家/地区的名称 - 不要认为用户会知道内部我们数据库的 ID:P,它说“模型值:100”但选择“300”。这是一支带有建议的笔 -> codepen.io/owenmelbz/pen/mPzbZM?editors=1010
猜你喜欢
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多