2016-04-04 4 views
0

Wie kann ich jedes Mal eine neue HTTP-Antwort erhalten, wenn ich zum Home-Tab zurückkomme?Wie ruft man die http-Anfrage auf jeder in AngularJS eingegebenen Seite auf?

Ich habe mehrere Dinge ausprobiert. Ich habe über "$ state.reload()" gelesen, aber ich weiß nicht, wo ich es verwenden soll. ng-click funktioniert nicht in tabs.html.

Controller:

.controller('HomeCtrl', function ($scope, $http) { 
     'use strict'; 

// how to call this every time on coming back to this tab? 
     $http.get("api.php") 
      .then(function successCallback(response) { 
       $scope.plan = response.data; 
      }, function errorCallback(response) { 
       console.error("Failed HTTP get: " + response.status + " " + response.data); 
      }); 
// ... 

tabs.html

<ion-tabs> 
    <ion-tab title="Home" href="#/tab/home"> 
    <ion-nav-view name="tab-home"></ion-nav-view> 
    </ion-tab> 
<!-- ... --> 
</ion-tabs> 

index.html:

<ion-nav-view></ion-nav-view> 

Modul:

.config(function ($stateProvider, $urlRouterProvider) { 
    $stateProvider 

     .state('tab', { 
      url: '/tab', 
      abstract: true, 
      templateUrl: 'templates/tabs.html' 
     }) 

     .state('tab.home', { 
      url: '/home', 
      views: { 
       'tab-home': { 
        templateUrl: 'templates/tab-home.html', 
        controller: 'HomeCtrl' 
       } 
      } 
     }) 

Vielen Dank!

+0

betrachten Sie 'angular.run', und dort abonnieren Sie' stateChangeStart', stellen Sie die Anfrage dort. –

Antwort

0

Kontroller

$scope.loadHttp = function(){ 
     $http.get("api.php") 
      .then(function successCallback(response) { 
       $scope.plan = response.data; 
      }, function errorCallback(response) { 
       console.error("Failed HTTP get: " + response.status + " " + response.data); 
      }); 

}; 

Im Hinblick zum Beispiel

<div ng-click ="loadHttp()">Tab</div> 
+0

ng-click funktioniert nicht in tabs.html-. – user3646958

+0

, weil Sie keinen Controller für Ihren Tab-Status angeben. – praHoc

+0

.state ('Reiter', { url: '/ Reiter', abstrakt: true, Controller: 'TabCtrl', templateUrl: 'templates/tabs.html' }) – user3646958

0

Benutzer on-Select.

<ion-tab 
    title="Home" 
    icon="my-icon" 
    href="#/tab/home" 
    on-select="onTabSelected()" 
    on-deselect="onTabDeselected()"> 
</ion-tab> 

Hier ist Demo

+0

Muss ich tabs.html löschen und etwas in index.html schreiben? – user3646958

0

Es scheint jetzt zu funktionieren. Danke an @praHoc.

Ich habe "Cache" dreimal hinzugefügt, sonst nichts.

 .state('tab', { 
      url: '/tab', 
      abstract: true, 
      cache: false, 
      templateUrl: 'templates/tabs.html' 
     }) 

     // Each tab has its own nav history stack: 

     .state('tab.home', { 
      url: '/home', 
      cache: false, 
      views: { 
       'tab-home': { 
        cache: false, 
        templateUrl: 'templates/tab-home.html', 
        controller: 'HomeCtrl', 
       } 
      } 
     })