0

我正在開發我的第一個AngularJS應用程序,並遇到了這個問題與jQuery查找功能。基本上我試圖做的是我有一個自定義的HTML組件。它包含使用ng-repeat指令的按鈕列表。每個都有它自己的ng-click監聽器,在它的回調函數中,我想查找所有的app-item元素,但是很遺憾,它只能返回第一個元素。AngularJS ng-repeat jQuery查找返回只有一個元素

我認爲這種情況我應該使用鏈接,但我不能自己來。 APP-screen.html的

部分:

<div>  
    <div ng-repeat="error in _errorData.errors"> 
     <button class="app-item" ng-click="_onApplicationContainerClick($event)"> 
      <div class="col-xs-4"> 
       {{error.date}} 
      </div> 
      <div class="col-xs-4"> 
       {{error.errorID}} 
      </div> 
      <div class="col-xs-3"> 
       {{error.message}} 
      </div> 
      <div class="col-xs-1"> 
       <span class="glyphicon glyphicon-collapse-down"></span> 
      </div> 
     </button> 
    </div> 
</div> 

AppScreen指令:

angular.module('ErrorViewer').directive('appScreen', function() { 
    return { 
     restrict: 'E', 
     templateUrl: 'src/view/scene/app-screen.html', 
     controller: 'AppScreenController' 
    }; 
}); 

AppScreenController的部分:

$scope._onApplicationContainerClick = function (e) { 
    // some executions 
    _collapseErrorData(); 
}; 

var _collapseErrorData = function() { 
    var elems = $element.find('.app-item'); 
    // Should return array of ng-repeated elements! 
} 
+0

並$元素來自何處而來?你試過angular.element.find('。app-item')嗎? – maurycy

+0

它只是控制器的一部分,它來自控制器聲明: 'angular.module('Smth').controller('AppScreenController',['$ document','$ scope','$ element', function ($ document,$ scope,$ element){' – MyFantasy512

+0

ok,那麼這個注入的'$ element'是什麼?它是jQuery,jQuery lit還是angular.element? – maurycy

回答

2

NG重複是在呈現階段呈現(當$ watch處理程序正在執行時)。在編譯和鏈接階段,您只能訪問模板。這就是你只看到一個元素的原因。

掛接到後呈現階段,您可以用$超時:

app.controller('AppScreenController', function($scope, $timeout) { 
    var _collapseErrorData = function() { 
     $timeout(function() { 
      var elems = $element.find('.app-item'); 
      // Should return array of ng-repeated elements! 
     }); 
    } 
}); 
+0

從我的角度來看,它應該在ng-repeat監視表達式發生了變化,而在我的案例中,_collapseErrorData沒有同時執行(因爲你不能單擊未被重新排列的元素),那麼這些元素不應該在舞臺上嗎? – MyFantasy512

相關問題