当前位置: 动力学知识库 > 问答 > 编程问答 >

Synchronize the parameter of a dynamic select list in AngularJS

问题描述:

I have a form with a dynamically generated select list. The values for this list are retrieved from the scope. The currently selected value is also retrieved from the scope.

Unfortunately, there is a synchronization issue.

Below this is illustrated. The dynamic select list shows 1 as selected, but the param is 2. The static select list shows the correct value.

angular.module('fiddle', [])

.controller('Ctrl', function($scope) {

$scope.xs = [1,2,3];

$scope.param = 2;

});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="fiddle" ng-controller="Ctrl">

Correct representation:

<select ng-model="param">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

</select><br>

Incorrect representation:

<select ng-model="param">

<option ng-repeat="x in xs" value="{{x}}">{{x}}</option>

</select><br>

param: {{param}}

</body>

网友答案:

As I was properly formulating my question, as often, I found the solution already.

I should use ng-options instead of ng-repeat on the option tag.

<select ng-model="param" ng-options="x for x in xs">
</select>

https://docs.angularjs.org/api/ng/directive/select

分享给朋友:
您可能感兴趣的文章:
随机阅读: