2016-09-26 81 views
0

我如何在下面的代碼中執行console.log(childResult)?在一般情況下/如何學習如何訪問ngRepeat作用域變量...不是那些在各自的控制器中(看起來很簡單)......但是,我想訪問LOOP VARIABLES例如childResult在我的情況。我已閱讀所有指南和教程,包括stackoverflow問題,但我無法解決它。如何在角度訪問循環變量NgRepeat

app.directive("appNamesTyped", ['$http', '$timeout', function ($http, $timeout) { 
    //app-names-typed 
    return { 
     restrict: 'A', 
     template: "<li ng-repeat=\"childResult in childrenLookupResults.d.as_typed | orderBy:'fname' | limitTo: limitLookupResults : beginLookupResults\"> </li>", 
     link: function (scope, element, attrs) { 
      try { 
       console.log(childResult); 
      } catch(er) { 

      } 
     } 
    }; 
}]); 
+0

您是否嘗試過 「的console.log(scope.childResult)」 – Aparna

回答

1

我會這麼做的方式是使用childrenLookupResults對象並循環遍歷它。它將打印元素而不考慮模板。這樣

link: function (scope, element, attrs) { 
    for(var i = 0; i < scope.childrenLookupResults.length; i++){ 
     console.log(scope.childrenLookupResults[i]); 
    } 
} 

東西這裏有一個相關的問題,可能對你會有所幫助:AngularJS: Setting a variable in a ng-repeat generated scope

0

另一種方式:

link: angular.forEach(scope.childrenLookupResults,function(childrenLookup){ 
      console.log(childrenLookup); 
     });