0

Ich bin neu in Angularjs und möchte einen Timer hinzufügen, sobald die Seite geladen wird. Ich habe hier einige ähnliche Fragen gelesen, aber ich konnte mein Problem nicht lösen. Ich habe data-ng-init="init()" meiner Ansicht hinzugefügt. Dies ist der Controller:

'use strict'; 

    angular.module('tempApp') 
     .controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) { 

     $scope.init = function(){ 
      console.log("Safety checkStatus page load"); 
      $timeout(callAtTimeout(),3000); 
     } 


    function callAtTimeout(){ 
     console.log("Safety checkStatus ***"); 
    } 

}]); 

jsfiddle: https://jsfiddle.net/Zusee/s6xy48t0/9/

Hier dService ist separater Service js Datei, die ich verwenden werde. Jede Hilfe wäre willkommen.

Antwort

0
'use strict'; 

     angular.module('tempApp') 
      .controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) { 

      $scope.init = function(){ 
       console.log("Safety checkStatus page load"); 
       $timeout(function() { 
        callAtTimeout() 
       }, 3000); 
      } }]); 


     function callAtTimeout(){ 
      console.log("Safety checkStatus ***"); 
     } 

    }]); 

Möglicherweise müssen Sie auch $ scope.init() explizit

+0

Vielen Dank für Ihre Unterstützung. Ich habe das auch ohne Glück versucht. –

+0

Bitte überprüfen Sie meine bearbeitete Antwort auf geänderte $ timeout() – urvashi

+0

@urvashi 'Ich habe hinzugefügt data-ng-init =" init() "Sie müssen es nicht explizit –

0

Add

.controller('MainController',['$scope','$timeout', 'dService', function ($scope,$timeout, dService) { 

Und wie

$timeout(callAtTimeout,3000); 
1

Sie dies versuchen, nutzen 'dService' nennen?

'use strict'; 

    angular.module('tempApp') 
     .controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) { 

     $scope.init = function(){ 

      console.log("Safety checkStatus page load"); 

      $timeout(function() { 
        $scope.callAtTimeout(); 
      }, 1000); 

     } 


     $scope.callAtTimeout = function(){ 
      console.log("Safety checkStatus ***"); 
     } 

     $scope.init(); 
}]); 
+0

Vielen Dank für Ihre Unterstützung. Aber es funktioniert immer noch nicht wie erwartet. –

+0

Entschuldigung. eine Frage ... warum benutzt du}]); zweimal ? –

+0

mm ... sorry Es ist Tippfehler. : –

0

Problem ist zusätzliche Klammer in $ scope.init()

$scope.init = function(){ 
    console.log("Safety checkStatus page load"); 
    $timeout(function() { 
      callAtTimeout(); 
    }, 3000); 
}; 
+0

Ich habe data-ng-init = "init()" aus meiner Sicht. –

+0

dann keine Notwendigkeit, Controller aufrufen, aber entfernen zusätzliche Klammern @ZuseeWeekin –

+0

mm ... sorry Es ist Tippfehler. :) –

1
<!DOCTYPE html> 
<html ng-app="tempApp"> 
<head> 
    <title>Test</title> 
    <script src="angular.min.js"></script> 
</head> 
<body ng-controller="MainController"> 
<script type="text/javascript"> 
     'use strict'; 

      angular.module('tempApp', []) 
      .controller('MainController',['$scope','$timeout', function ($scope,$timeout) { 

       $scope.init = function(){ 
        console.log("Safety checkStatus page load"); 

        $timeout(callAtTimeout, 3000); 
       }; 
       function callAtTimeout(){ 
         console.log("Safety checkStatus ***"); 
        } 

$scope.init(); 
     }]); 
</script> 
</body> 
</html> 
0

Warum Sie $timeout benötigen, wenn Sie Timer einstellen möchten.

$timeout wird einmal ausgeführt.

Versuchen Sie mit $interval. Hier

sind die Änderungen:

$scope.init = function() { 
      console.log("Safety checkStatus page load"); 
      $interval($scope.callAtTimeout, 1000); 
     } 
     $scope.callAtTimeout = function() { 

      console.log("Safety checkStatus ***"); 
     } 

Hier ist der Arbeits plunker

Verwandte Themen