AngularJS动态ng选项

时间:2016-02-24 13:34:17

标签: angularjs

我有 选择

<select ng-model="country" ng-options="{{selectOptions}}">
        <option style="display:none" value="">Select country</option>
</select>

和json国家/地区列表{id:...,nameEn:...,nameRu:...}

有人可以解释为什么会有效:

$scope.lang = "En";
$scope.selectOptions = "country as country['name'+lang] for country in countries";
$scope.customerChanged = function(v){
    if (/[а-яА-ЯЁё]/.test(v))
        $scope.lang = "Ru";
    else
        $scope.lang = "En";
}

但这并不是:

$scope.selectOptions = "country as country['nameEn'] for country in countries";
$scope.customerChanged = function(v){
    if (/[а-яА-ЯЁё]/.test(v))
        $scope.selectOptions = "country as country['nameRu'] for country in countries";
    else
        $scope.selectOptions = "country as country['nameEn'] for country in countries";
}

这不起作用:

<select ng-model="country" ng-options="country as country.name{{lang}} for country in countries">
        <option style="display:none" value="">Select country</option>
</select>




$scope.lang = "En";
$scope.customerChanged = function(v){
    if (/[а-яА-ЯЁё]/.test(v))
        $scope.lang = "Ru";
    else
        $scope.lang = "En";
}

2 个答案:

答案 0 :(得分:0)

<!--First Solution-->
<select ng-model="selectValue" ng-options="item.Name for item in items">
    <option selected value="">Select Value</option>
</select>

<!--Second Solution-->
<select ng-model="selectValue">
    <option  value="{{item.Value}}"  ng-repeat="item in items">
        {{item.Name}}
    </option>
</select>

<!--Third Solution-->
<select ng-model="selectValue" ng-options="item.Name for item in items">
</select>

<!--For Multiple Select--> 
<select ng-model="" multiple ng-options="">
</select>

答案 1 :(得分:0)

来自角度文档:

  

为什么混合插值和表达式是不好的做法:

     

无法保证它适用于每个指令,因为   插值本身就是一个指令。如果另一个指令访问   插值运行前的属性数据,它将获得原始数据   插值标记而不是数据。