AngularJs无法通过ng-repeat

时间:2015-07-20 09:09:29

标签: angularjs

我需要预先选择由ng-repeat

呈现的选择
<body ng-app ng-controller="AppCtrl">
    <div>Operator is: {{openAccount.leverage}}</div>
    <select class="form-control" name="leverage" ng-model="openAccount.leverage">
        <option ng-repeat="leverage in currentType.leverageList"
                value="{{leverage}}" ng-selected="{{currentType.defaultLeverage == openAccount.leverage}}">1:{{leverage}}
        </option>
    </select>
</body>

function AppCtrl($scope) {
    $scope.currentType = {};
    $scope.currentType.leverageList = ['10','20','30'];
    $scope.currentType.defaultLeverage = '20';
}

这是我的代码,这里是小提琴 http://jsfiddle.net/FxM3B/440/

3 个答案:

答案 0 :(得分:3)

下面的代码对我有用。

<option 
    ng-repeat="leverage in currentType.leverageList"
    value="{{leverage}}"
    ng-selected="{{currentType.defaultLeverage == leverage}}">1:{{leverage}}</option>

fiddle

答案 1 :(得分:0)

您应该尝试使用ngOptions指令重复select选项。

<select ng-options="item.subItem as item.label for item in values track by item.id" ng-model="selected">

AngularJS文档中的示例。

答案 2 :(得分:0)

HTML:

<body ng-app ng-controller="AppCtrl">
  <div>Operator is: {{openAccount.leverage}}</div>
  <select class="form-control" name="leverage" ng-model="openAccount.leverage">
  <option 
   ng-repeat="leverage in currentType.leverageList"
   value="{{leverage.value}}"
  ng-selected="{{openAccount.leverage == leverage.value}}"{{leverage.value}}</option>
</select>
</body>

使用Javascript:

function AppCtrl($scope) {
    $scope.openAccount ={
        leverage : '20'
    }
    $scope.currentType = {};
    $scope.currentType.leverageList = [
         {value: '10', displayName: '10'},
         {value: '20', displayName: '20'},        
         {value: '30', displayName: '30'} 
    ]      
 }

FIDDLE