2016-08-02 13 views
0

Ich bin ziemlich neu in eckigen. Ich habe meine Direktive und ich würde meine Timer() Funktion für jedes Objekt ausführen.Einmaliger Timer für jede Direktive

Mein Controller mit Daten:

app.controller("cardsCtrl", function($scope, $interval){ 

     $scope.changeTire = { 
      name: 'Change Tire', 
      timer: 16 
      }; 
      $scope.fixTire = { 
      name: 'Fix Tire', 
      timer: 30 
      }; 

     function timer(){ 
      $interval(function(){ 
       if($scope.countDown>0){ 
        $scope.countDown--; 
        $scope.cardAnimation = {'width': +($scope.countDown/fullTimer*100)+'%' } 
       } 
      },1000,0); 
     } 

}); 

Meine Richtlinie:

<div class="card"> 
     <span class="name">{{cardInfo.name}}</span> 
     <span class="time">{{cardInfo.timer }}</span> 
     <div class="card-inner" ng-style="cardAnimation" ng-hide="finishCard"></div> 
    </div> 

var app = angular.module ("Karten", [ 'ngAnimate']);

app.directive("card", function(){ 
    return{ 
     restrict: 'E', 
     scope: { 
      cardInfo: '=info' 
     }, 
     templateUrl: 'card.html' 
    }; 
}); 

und die HTML-

<div ng-app="cards" ng-controller="cardsCtrl"> 
    <card info="changeTire" ></card> 
    <card info="fixTire" ></card> 
</div> 

Antwort

1

ein Weg, dies zu tun ist, fungiert als Rückruf die Richtlinie übergeben und diese Funktion in der Richtlinie auszuführen.

Controller:

app.controller("cardsCtrl", function($scope, $interval){ 

    $scope.changeTire = { 
     name: 'Change Tire', 
     timer: 16, 
     timerFunction: timerFunct 
     }; 
     $scope.fixTire = { 
     name: 'Fix Tire', 
     timer: 30, 
     timerFunction: timerFunct 
     }; 

    function timerFunct(){ 
     $interval(function(){ 
      if($scope.countDown>0){ 
       $scope.countDown--; 
       $scope.cardAnimation = {'width': +($scope.countDown/fullTimer*100)+'%' } 
      } 
     },1000,0); 
    } 

}); 

und in Richtlinie:

app.directive("card", function(){ 
    return{ 
     restrict: 'E', 
     scope: { 
      cardInfo: '=info' 
     }, 
     templateUrl: 'card.html', 
     link: function(scope){ 
      scope.cardInfo.timerFunction() 
     } 
    }; 
}); 

statt obigen Code tun.

Controller:

app.controller("cardsCtrl", function ($scope, $interval) { 

    $scope.changeTire = { 
     name: 'Change Tire', 
     timer: 16 
    }; 

    $scope.fixTire = { 
     name: 'Fix Tire', 
     timer: 30 
    }; 
}); 

und in Richtlinie:

app.directive("card", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      cardInfo: '=info' 
     }, 
     templateUrl: 'card.html', 
     link: function (scope) { 
      timerFunct(scope.cardInfo.timer); 

      function timerFunct(timer) { 
       $interval(function() { 
        if (timer > 0) { 
         timer--; 
         $scope.cardAnimation = { 'width': +(timer/fullTimer * 100) + '%' } 
        } 
       }, 1000, 0); 
      } 
     } 
    }; 
}); 
+0

eine weitere Sache, wie kann ich Timer-Variable in timerFunct zugreifen, so wird das Intervall auf geeigneten Wert eingestellt wird – Higeath

+0

ich meine Antwort lassen bearbeiten für Sie warten. –

+0

für Intervall zu arbeiten es erfordert Zugriff auf Intervall wie ich in der Controller-Funktion (Score, Intervall) – Higeath

Verwandte Themen