2014-02-13 25 views
0

關於如何做到這一點的任何想法?在angularjs中自動選擇第一個ng-repeat元素?

我基本上1個控制器設置,如果你點擊一個元素,動畫上面出現,然而,的第一要素應該是積極

一些/只要在頁面加載點擊...代碼:

動畫出現在這裏:

<div class="animation"> 
    <div ng-repeat="theme in themes" ng-show="isActive($index);" ng-if="isActive($index);"> 
     <img id="id_{{ theme.animation_id }}" ng-src="{{ theme.animation_gif_url }}" class="active" /> 
    </div> 
</div> 

動畫這裏選擇:

<div class="theme-select animated fadeInUp"> 
    <div class="theme-container" ng-repeat="theme in themes" ng-click="showAnimation($index);"> 
     <div id="theme_{{ theme.animation_id }}" class="theme"> 
      <img ng-src="{{ theme.icon_url }}" /> 
      <div class="name"> 
       {{ theme.name }} 
      </div> 
     </div> 
    </div> 
</div> 

這裏是我的控制器:

themeFunction(function(themeData) { 
     name = []; 
     themeID = []; 
     animationURL = []; 
     var themes = $scope.themes = themeData.themes; 
     for (var t = 0; t < themes.length; t++) { 
      animationURL = themes[t].animation_url; 
      themeID = themes[t].animation_id; 
      iconURL = themes[t].icon_url; 
      name = themes[t].name; 
      animationGifUrl = themes[t].animation_gif_url; 
      $scope.themeID = themeID; 
      if ($scope.themes[t].animation_gif_url == 'NULL' || $scope.themes[t].animation_gif_url == null || $scope.themes[t].animation_gif_url == '.gif') { 
       // console.log(name + " are null or weird"); 
       $scope.themes[t].animation_gif_url = iconURL; 
      } else { 
       $scope.themes[t].animation_gif_url = animationGifUrl; 
      } 
     } 
     $scope._Index = 0; 
     $scope.isActive = function(index) { 
      return $scope._Index === index; 
     }; 
     $scope.showAnimation = function(index) { 
      selectedTheme = $(this).attr('theme'); 
      console.log('Selected ' + selectedTheme.animation_id); 
      $scope._Index = index; 
     }; 
    }); 

回答

1

在您的控制器,您的列表加載完成後,請致電showAnimation第一指標

...list loads up 
$scope.themes = data; 
if (data.length) $scope.showAnimation(0); 
0

因爲:

[] == false 

所以,你可以做if只是這樣的:

if (themes) $scope.showAnimation(0); 

如果主題是空數組,沒有事情會發生。

如果主題是一個數組有至少一個主題,將觸發showAnimation函數

相關問題