简体   繁体   中英

Angularjs - setTimeout function in ng-init

i can use two function on ng-init ? how? i've tried an setTimeout to run an function to open a modal and after a time period , it call the function that closes the modal , but does not work ...

like this :

ng-init="setTimeout(closeModal, 2000) && openModal();"

both functions are inside the controller:

function ($scope, $stateParams, $ionicModal, $window, $timeout ) {




  $ionicModal.fromTemplateUrl('modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function() {
      alert("funcionou!");
    //$scope.modal.show();
      $timeout(function(){
$scope.modal.show(); 

},0)

  };

  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  // Cleanup the modal when we're done with it!
  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });
  // Execute action on hide modal
  $scope.$on('modal.hidden', function() {
    // Execute action
  });
  // Execute action on remove modal
  $scope.$on('modal.removed', function() {
    // Execute action
  });
ng-init="initialFunction()"

Inside your controller write:

function initialFunction(){
$timeout(closeModal, 2000); openModal();
}

First use $timeout instead of setTimeout .

second use ; instead of && at the end of each function when calling multiple functions

ng-init="$timeout(closeModal, 2000); openModal();"

add this to your controller

$scope.$timeout = $timeout; 

inject the $timeout to the controller also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM