刷新控制器中的多个页面

时间:2017-07-12 10:35:54

标签: angularjs ionic-framework

我想知道是否可以在单个控制器中更新多个页面? 我需要在将数据添加到列表后刷新我的主页。 现在我在我的主页上使用了一个pull来刷新,但我想在我的列表中添加一个后更新。

我的控制员:

.controller('addCRCtrl', function ($scope, $state, $stateParams, $ionicPopup, $ionicLoading, $timeout, AppService) {

let loadData = () => {
  $ionicLoading.show();
  $scope.loading=true;
  $scope.form = {};
  $scope.formProjet = {};
  $scope.form.patient = idjeune;
  $scope.form.dateVisite = new Date();
  AppService.patient(idjeune).then(function (response) {
    $scope.patient = response;
    console.log(response);
  });
  AppService.user().then(function (response) {
    $scope.bene1 = response;
    console.log(response);
  });
  AppService.hopitaux().then(function (response) {
    $scope.hopitaux = response;
  });

  AppService.lieuxlist().then(function (response) {
    $scope.lieux = response;
  });
  AppService.userByAntenneAddCR().then(function (response) {
    console.log(response);
    $scope.listeBene = JSON.parse(JSON.stringify(response));
  });
  AppService.typeVisite().then(function (response) {
    $scope.listeVisite = JSON.parse(JSON.stringify(response));
  });
  AppService.projet(idjeune).then(function (response) {
    $scope.projets = response;
  });
  AppService.getCategorie().then(function (response) {
    $scope.categorieprojet = response;
    console.log(response);
    if (update) {
      $scope.updating = update;
      $scope.form.dateVisite = new Date(cr.date);

      for (let i in $scope.lieux) {
        if ($scope.lieux[i].id == cr.idLieu) {
          $scope.lieu = $scope.lieux[i];
          break;
        }
      }
      $scope.form.lieudetail = cr.lieudetail;
      $scope.bene1 = {
        id: cr.bene1,
        nom: cr.benevole1
      }
      $scope.bene2 = {
        id: cr.bene2,
        nom: cr.benevole2
      }
      $scope.bene3 = {
        id: cr.bene3,
        nom: cr.benevole3
      }
      console.log("Benevole 1 : ", $scope.bene1);
      console.log("Benevole 2 : ", $scope.bene2);
      console.log("Benevole 3 : ", $scope.bene3);
      $scope.form.description = cr.compteRenduVisite;
      $scope.form.todo = cr.todo;
      $scope.form.etatpatient = cr.etatPatient;
      for (let i in $scope.listeVisite) {
        if ($scope.listeVisite[i].id == cr.typeVisite) {
          $scope.typeVisite = $scope.listeVisite[i];
          break;
        }
      }
      for (let i in $scope.projets) {
        if ($scope.projets[i].id == cr.idProjet) {
          $scope.projet = $scope.projets[i];
          break;
        }
      }
      $timeout(() => { $ionicLoading.hide(); $scope.loading=false;;}, 6000);
    }
    else
      $timeout(() => { $ionicLoading.hide(); $scope.loading=false;}, 2000);
  });
}
$scope.$on('$ionicView.beforeEnter', function (event, viewData) {   
  loadData();
});
$scope.addCr = function () {
  $ionicLoading.show();
  let isValid = $scope.form.dateVisite && $scope.patient && $scope.bene1 && $scope.form.description && $scope.form.todo;
  isValid = isValid && $scope.form.etatpatient && $scope.typeVisite;
  let formInvalid = () => {
    $ionicLoading.hide();
    var alertPopup = $ionicPopup.alert({
      template: '<p style="text-align: center;">Veuillez renseigner tous les champs</p>'
    });
  }
  if (!isValid) {
    formInvalid();
    return;
  }
  $scope.form.lieu =  ($scope.lieu)? $scope.lieu.id:0;
  $scope.form.bene1 = $scope.bene1.id;
  $scope.form.bene2 = ($scope.bene2) ? $scope.bene2.id : null;
  $scope.form.bene3 = ($scope.bene3) ? $scope.bene3.id : null;
  $scope.form.lieudetail = ($scope.form.lieudetail) ? $scope.form.lieudetail : "-";

  let temp = $scope.form.dateVisite;
  //$scope.form.dateVisite = $scope.form.dateVisite.toISOString().slice(0, 10);
  let newProject = false;
  $scope.form.typeVisite = $scope.typeVisite.id;
  switch ($scope.typeVisite.id) {
    case 3:
      newProject = true;
      isValid = isValid && $scope.cprojet && $scope.formProjet.description && $scope.formProjet.titre;
      break;
    case 4:
    case 5:
      if ($scope.projet) $scope.form.projet = $scope.projet.id;
      isValid = isValid && $scope.projet;
      break;

    default:
      $scope.form.projet = 0;
      break;
  }

  for (var i in $scope.hopitaux) {
    if ($scope.hopitaux[i].nom === $scope.patient.hopital) {
      $scope.form.hopital = $scope.hopitaux[i].id;
    }
  }
  for (var j in $scope.form) {
    if ($scope.form[j] == null) {
      if (j == 'bene2' || j == 'bene3')
        $scope.form[j] = 0;
      else
        delete $scope.form[j];
    }
  }
  if (!isValid) {
    formInvalid();
    return;
  }
  let callService = () => {
    var myform = angular.copy($scope.form);
    if (update)
      myform.dateVisite = new Date($scope.form.dateVisite.getTime() + 86400000).toISOString().slice(0, 10);
    else
      myform.dateVisite = $scope.form.dateVisite.toISOString().slice(0, 10);
    console.log(myform);
    let idtogo = (update) ? cr.id : null;
    AppService.addCR(myform, idtogo).then(function (response) {
      update = false;
      cr = null;
      var alertPopup = $ionicPopup.alert({
        title: 'Ajout Réussie !',
        template: '<p style="text-align: center;">Merci d\'avoir renseigné les informations relatives à la visite !</p>'
      });
      $scope.form.dateVisite = temp;
      console.log(response);
      $ionicLoading.hide();
      $state.reload('menu.ficheJeune');
      $state.go('menu.ficheJeune');
    });
  }
  if (!newProject || update)
    callService();
  else {
    $scope.formProjet.categorie = $scope.cprojet.id;
    $scope.formProjet.patient = idjeune;
    var myprojectform = angular.copy($scope.formProjet);
    console.log(myprojectform);
    AppService.addProject(myprojectform).then((response) => {
      console.log(response);
      if (response) {
        AppService.projet(idjeune).then(function (response) {
          for (let i in response) {
            console.log("Response :", response);
            let temp = false;
            for (let j in $scope.projets) {
              if (response[i].id == $scope.projets[j].id) {
                temp = true;
              }
            }
            if (!temp) {
              $scope.form.projet = response[i].id;
              callService();
              break;
            }
          }
        });
      }
    });
  }
};
AppService.refreshToken().then(function (response) {
  if ($scope.token) {
    console.info('Refresh');
  }
});
})

目前,要更新我的第一页:

$state.reload('menu.ficheJeune');
$state.go('menu.ficheJeune');

但我还想更新名为'menu.home'的第二页。

我提前感谢你

1 个答案:

答案 0 :(得分:1)

您可以在homeCtrl中使用它:

$scope.$on('$ionicView.beforeEnter', function() {
  console.log('beforeEnter');
  // your code
});