2013-11-27 59 views
0

我是Angular的新手,我想做一個非常基本的例子。 在用ng-repeat顯示一些數據之後,我想使用一些帶有javascript函數的數據。但是我不知道什麼時候執行javascript,因爲我不會在角度完成時顯示我的數據。ng-repeat後觸發函數

Here is a fiddle:我嘗試使用函數getAssign3顯示Angular顯示的一些數據。我應該什麼時候調用這個函數?

代碼:

<div ng-app="Test"> 
    <h1>Results Table</h1> 
    <div ng-controller="AssignmentCtrl"> 
      <span ng-repeat="assign in assignments"> 
      <span id="{{assign.id}}">{{assign.title}}</span> 
       <br/> 
     </span> 
</div> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script> 

更多代碼:

var test = angular.module("Test", []); 

function AssignmentCtrl($scope, $http, $timeout) { 
    $scope.fetch = function() { 
     // -- Mock server data.. 
     var data = [{id: 1,title: "Math 1", 
        },{id: 2, title: "Math 2", 
        }, { id: 3,title: "Math 3", 
        }]; 
     $scope.assignments = data; 
    }; 
    $scope.fetch(); 
} 

getAssign3 = function(){ 
    var dd = $("#3").text(); 
    alert(dd); 
} 
getAssign3(); 
+1

[調用時NG-重複完成的功能]的可能重複(http://stackoverflow.com/questions/15207788/calling-a-function-when-ng -repeat-has-finished) – Stewie

+0

檢查這個帖子[http://stackoverflow.com/questions/13471129/angularjs-ng-repeat-finish-event] – Ravi

回答

0

你必須在你的控制器上添加一個捕手$:

$scope.$on('ngRepeatFinished'), function (ngRepeatFinishedEvent) { 
     console.log "After loop"; 
} 

一個指令在app.js:

.directive('onFinishRender', function($timeout) { 
    return { 
     restrict: 'A', 
     link: function(scope, element, attr) { 
     if (scope.$last) { 
      return $timeout (function() { 
      scope.$emit 'ngRepeatFinished' 
      }); 
     } 
    } 
}) 

,並在您的視圖:

on-finish-render="myFunc()"