选择选项中的角度ng-repeat显示额外空间而不选择默认值。

时间:2016-10-13 09:19:46

标签: javascript angularjs

我试图通过角度ng-repeat在选择框中显示年份列表,但它在年份列表之前显示一个空行,而不显示应该是列表中第一年的选定值。你可以在代码示例中看到。任何帮助将受到高度赞赏。

 angular.module('F1FeederApp', ['F1FeederApp.controllers']);
 
  angular.module('F1FeederApp.controllers', []).

  /* Drivers controller */
  controller('driversController', function($scope) {
  
    $scope.years = getYearRange(); 
   	
  });
  /* Year Range Code */
  function getYearRange() {
  var startYear = new Date().getFullYear();
  var endYear = 2005;
  var dateRange = [];

  while(endYear <= startYear) {
      dateRange.push(startYear);
      startYear -= 1
  }
  return dateRange;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<div ng-app="F1FeederApp">
  <div ng-controller="driversController">  
      <select ng-model="F1s_Selection" class="btn btn-default">
       <option ng-repeat="year in years">{{year}}</option>
      </select>
</div>
  </div>

4 个答案:

答案 0 :(得分:2)

使用ng-options而不是// Use custom query string wp_enqueue_script('myjs', $pluginpath . 'build/js/app.min.js', array('jquery'), '1.2.3'); 它有效,而且效果更好。

&#13;
&#13;
ng-repeat
&#13;
angular.module('F1FeederApp', ['F1FeederApp.controllers']);
angular.module('F1FeederApp.controllers', []).

  /* Drivers controller */
  controller('driversController', function($scope) {
  
    $scope.years = getYearRange(); 
   	
  });
  /* Year Range Code */
  function getYearRange() {
  var startYear = new Date().getFullYear();
  var endYear = 2005;
  var dateRange = [];

  while(endYear <= startYear) {
      dateRange.push(startYear);
      startYear -= 1
  }
  return dateRange;
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

空行是您默认选择的值,因为您的模型为空。 要获得默认的选定值,您需要为模型增加一个值:

$RegKey = “HKLM:\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\WINLOGON”
if (-Not(Test-Path “$RegKey”)) {
New-Item -Path “$($RegKey.TrimEnd($RegKey.Split(‘\’)[-1]))” -Name “$($RegKey.Split(‘\’)[-1])” -Force | Out-Null
 }
 Set-ItemProperty -Path “$RegKey” -Name “DefaultPassword” -Type String -Value “passwordvalue”

而@ Alexis的答案也使用$scope.years = getYearRange(); $scope.F1s_Selection = $scope.years[0]; 代替ng-options。它有更好的表现。

答案 2 :(得分:0)

请尝试ng-init。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<div ng-app="F1FeederApp">
  <div ng-controller="driversController">  
      <select ng-init = "F1s_Selection = years[0]" ng-model="F1s_Selection" class="btn btn-default">
       <option ng-repeat="year in years">{{year}}</option>
      </select>
</div>
  </div>

答案 3 :(得分:0)

角度方式是使用ng-options:

WARN HHH000342: Could not obtain connection to query metadata : Login failed for user 'my_user'.

并在控制器集<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> <div ng-app="F1FeederApp"> <div ng-controller="driversController"> <select ng-model="F1s_Selection" class="btn btn-default" ng-options"year in years">{{year}}</option> </select> </div> </div> 中设置了所需的默认值,如下所示:

F1s_Selection