2013-08-27 22 views
0

我創建與tabletop.js,handlebars.js Web應用程序,並angular.js數據消失時,URL路徑

所以數據從谷歌電子表格的讀取和路由 採用了棱角分明,但是當路線後,另一頁,回來的數據就會消失

Here's a Plunker of it

有人可以給一個解決方案請,我已經嘗試過搜索,但沒有運氣尚未:/

桌面控制器

var public_spreadsheet_url = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AkLy8qCw6J5EdGlmeFZEVVVZZ3ZUSEhYcjhEdEZKelE&output=html'; 

    $(document).ready(function() { 
    Tabletop.init({ key: public_spreadsheet_url, 
        callback: showInfo, 
        parseNumbers: true }); 
    }); 

    function showInfo(data, tabletop) { 
    var source = $("#cat-template").html(); 
    var template = Handlebars.compile(source); 

    $.each(tabletop.sheets("Cats").all(), function(i, cat) { 
     var html = template(cat); 
     $("#content").append(html); 
    }); 
    } 

而這些角度腳本

var routeApp = angular.module('routeApp', []); 

    routeApp.filter('range', function() { 
     return function(input, total) { 
      total = parseInt(total); 
      for (var i=0; i<total; i++) { 
       input.push(i); 
      } 
      return input; 
     }; 
    }); 

    routeApp.config(function($routeProvider, $locationProvider) { 

     $routeProvider. 
      when('/home', { controller: HomeCtrl, templateUrl: 'partials/home.html' }). 
      when('/movie/:id', { controller: BlogDetailCtrl, templateUrl: 'partials/movieDetail.html' }). 
      when('/product', { controller: ProductCtrl, templateUrl: 'partials/product.html' }).     
      otherwise({ redirectTo: '/home' }); 

     $locationProvider.html5Mode(false); 

    }); 

    function MainCtrl($scope) { 

     $scope.navs = [ 
      { text: 'Products', href: '#/product' },     
     ]; 

    } 

    function HomeCtrl($scope) {   
     console.log($scope);    
    } 

    function BlogDetailCtrl($scope, $routeParams) { 
     $scope.id = $routeParams.id; 

     $scope.youAreClick = function(what) { 
      alert('You are click on ' + what); 
     } 
    } 

    function ProductCtrl($scope) {   
     $scope.items = [ 
      { grid: 3.5, filename: '360x270' }, 
      { grid: 3.5, filename: '260x120' }, 
      { grid: 3, filename: '160x120' }, 
      { grid: 3, filename: '260x120' }, 
      { grid: 3, filename: '260x120' } 
     ];   
    } 
+0

你能提供你的控制器代碼嗎? – BKM

+0

@BKM主題更新:) – DarKneT

回答

1

控制器創建/銷燬每次飛行路線的變化。存儲在它們中的數據不是持久的。

克服此問題的一種方法是使用Service

如文檔中所述:「最後,認識到所有的Angular服務都是應用程序單例很重要。」

+0

Sergiu是正確的 - 使用服務來存儲您的數據並將其注入到您需要的控制器中。 – BoxerBucks

+0

似乎現在工作,但得到一些問題緩存(不知道這個或不是)因爲 – DarKneT

+0

您使用的是資源嗎?如果是這樣,然後檢查出http://docs.angularjs.org/api/ng.$cacheFactory –

0

結賬Angular-Multi-View,特別是persist()方法。它可以讓您在路由更改中保留視圖的範圍。

+0

我會這個試試也謝謝 – DarKneT