2016-01-23 11 views
0

我的狀態在我的app.js:

.state('app.kamus', { 
    url: '/kamus', 
    views: { 
     'menuContent': { 
      templateUrl: 'templates/kamus.html', 
      controller: 'Kamus1Ctrl' 
     } 
    } 
}) 
... 

和:

$urlRouterProvider.otherwise('/app/kamus'); 

控制器:

.controller('Kamus1Ctrl', function($scope, $cordovaSQLite, $ionicLoading, $stateParams, $timeout) { 

$ionicLoading.show({ 
    content: 'Loading', 
    animation: 'fade-in', 
    showBackdrop: true, 
    maxWidth: 200, 
    showDelay: 0 
}); 

$scope.selectAll = function() { 
    $scope.datas = []; 
    var query = "SELECT * FROM data"; 
    $cordovaSQLite.execute(db, query, []).then(function(res) { 
     $scope.datas = []; 
     if (res.rows.length > 0) { 
      for (var i = 0; i < res.rows.length; i++) { 

       $scope.datas.push(res.rows.item(i)); 
      } 
     } else { 
      console.log("No results found"); 
     } 
    }).finally(function() { 
     $ionicLoading.hide(); 
    }); 
} 
$scope.selectAll(); 

})

我將$ urlRouterProvider.otherwise指向/ app/kamus,以便在首次啓動我的應用程序時顯示kamus頁面。但是看起來$ urlRouterProvider.otherwise在我的Kamus1Ctrl的selectAll()函數中執行$ cordovaSQLite.execute(db,query,[])失敗。但是,當我使用其他頁面作爲我的$ urlRouterProvider.otherwise,即:$ urlRouterProvider.otherwise('app/other'),然後通過我的側邊菜單進入/ app/kamus(按鈕ui-sref =「 app.kamus「),它只是起作用。

如何解決這個問題?

+0

你在說什麼功能?準確地說,你期望會發生什麼,而恰恰是發生了什麼?發佈相關代碼和錯誤堆棧跟蹤(如果有的話)。 –

+0

你好,我剛剛更新了我的帖子。我希望selectAll()函數在我的應用程序首次啓動時被調用。錯誤是因爲selectAll()函數在使用$ urlRouterProvider.otherwise('/ app/kamus')時無法識別,因此離子加載不會被隱藏。 – fxbayuanggara

+0

您如何診斷它未被調用?會發生什麼呢? –

回答

0

做這樣

的Index.html

<body> 
    <div> 
    <h3>Index page</h3> 
     <div ui-view="main"></div> 
    </div> 
</body> 

kamus.html

<div ui-view="view1"></div> 

控制器

var routerApp = angular.module('routerApp', ['ui.router']); 

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

    $urlRouterProvider.otherwise('app'); 

    $stateProvider  

    .state('app', { 
     abstract: true, 
     url: '/app', 
     views: { 
      'main': { 
       templateUrl: 'kamus.html', 
       controller : 'Kamus1Ctrl' 
      } 
     } 
    }) 
    .state('app.kamus', { 
     url: '', 
     views: { 
      '[email protected]': { 
       template: "Im View1" 
      } 
     } 
    }); 

}); 

routerApp.controller('Kamus1Ctrl', function($scope, $cordovaSQLite, $ionicLoading, $stateParams, $timeout) { 
alert("Inside controller"); 
    $scope.selectAll = function() { 
     alert("Inside selectAll"); 
     //rest of the code 
    } 

    $scope.selectAll(); 
    }) 

注意:在ui路由器點運算符用於內部狀態。

+0

我有嘗試這個到我的項目有幾個調整,但$ cordovaSQLite.execute(數據庫,查詢,[])仍然無法訪問。但控制器和selectAll()函數正在工作。 – fxbayuanggara

+0

我對離子和科爾多瓦沒有經驗,所以在這個問題中增加了cordova標籤。我希望你能從他們那裏得到幫助。 –