使用 ng-repeat 生成选择选项(附 Demo)

Jor*_*ink 2 angularjs angularjs-ng-repeat angular-ngmodel

控制器:

$scope.send_index = function(event, val){ 
    console.log(val);
    $scope.variable = 'vm.areas['+val+'].name';
    console.log( $scope.variable ); 
};
Run Code Online (Sandbox Code Playgroud)

这是在视图中:

<select ng-model="vm.areas">
    <option ng-repeat="area in vm.areas"  ng-value="area" id="{{area}}" ng-click="send_index($event, $index)">{{area.name}}
</select>
<input value="{{variable}}">
Run Code Online (Sandbox Code Playgroud)

其中“vm”是我的模型。


表达式内的表达式(Angular)

我的问题是我需要一个 {{array[]}},将索引作为另一个表达式,

例如:{{Array[{{val}}]}}。

已经尝试过这个:

 $scope.variable = ''+'vm.areas['+val+'].name'+'';
Run Code Online (Sandbox Code Playgroud)

问题出在视图中,“变量”显示为字符串

(vm.areas[0].name)

它没有获得该查询的勇气。

geo*_*awg 5

这是不正确的使用方法ng-model<select>在AngularJS元素:

<!-- ERRONEOUS
<select ng-model="vm.areas">
    <option ng-repeat="area in vm.areas"  ng-value="area" id="{{area}}" ng-click="send_index($event, $index)">{{area.name}}
</select>

<input value="{{variable}}">
-->
Run Code Online (Sandbox Code Playgroud)

无需使用该ng-click指令。该选择指令自动处理这一点。该ng-model指令接收或设置所选选项。

看:


<!-- ERRONEOUS
<select ng-model="vm.areas">
    <option ng-repeat="area in vm.areas"  ng-value="area" id="{{area}}" ng-click="send_index($event, $index)">{{area.name}}
</select>

<input value="{{variable}}">
-->
Run Code Online (Sandbox Code Playgroud)
angular.module('ngrepeatSelect', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.data = {
     model: null,
     availableOptions: [
       {id: '1', name: 'Option A'},
       {id: '2', name: 'Option B'},
       {id: '3', name: 'Option C'}
     ]
    };
 }]);
Run Code Online (Sandbox Code Playgroud)