2015-07-12 104 views
0

我開始構建離子應用程序,但是我有一個嚴重的路由問題!我添加了一個按鈕,但它鏈接到一個空白頁面,並沒有顯示該頁面的內容!離子路由問題,在鏈接中顯示空白頁面

我有3個文件:

app.js

// Ionic Starter App 

// angular.module is a global place for creating, registering and retrieving Angular modules 
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html) 
// the 2nd parameter is an array of 'requires' 
// 'starter.services' is found in services.js 
// 'starter.controllers' is found in controllers.js 
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services']) 

.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleLightContent(); 
    } 
    }); 
}) 

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

    // Ionic uses AngularUI Router which uses the concept of states 
    // Learn more here: https://github.com/angular-ui/ui-router 
    // Set up the various states which the app can be in. 
    // Each state's controller can be found in controllers.js 
    $stateProvider 

    // setup an abstract state for the tabs directive 
    .state('tab', { 
    url: "/tab", 
    abstract: true, 
    templateUrl: "templates/tabs.html" 
    }) 

    // Each tab has its own nav history stack: 

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

    .state('tab.chats', { 
     url: '/chats', 
     views: { 
     'tab-chats': { 
      templateUrl: 'templates/tab-chats.html', 
      controller: 'ChatsCtrl' 
     } 
     } 
    }) 
    .state('tab.chat-detail', { 
     url: '/chats/:chatId', 
     views: { 
     'tab-chats': { 
      templateUrl: 'templates/chat-detail.html', 
      controller: 'ChatDetailCtrl' 
     } 
     } 
    }) 

    $stateProvider.state('page', { 
    url: '/templates/page', 
    views: { 
    home: { 
     templateUrl: 'templates/page.html', 
     controller: 'PageCtrl' 
    } 
    } 
}) 

    .state('tab.account', { 
    url: '/account', 
    views: { 
     'tab-account': { 
     templateUrl: 'templates/tab-account.html', 
     controller: 'AccountCtrl' 
     } 
    } 
    }); 

    // if none of the above states are matched, use this as the fallback 
    $urlRouterProvider.otherwise('/tab/dash'); 

}); 

controllers.js

angular.module('starter.controllers', []) 

.controller('DashCtrl', function($scope) {}) 

.controller('ChatsCtrl', function($scope, Chats) { 
    // With the new view caching in Ionic, Controllers are only called 
    // when they are recreated or on app start, instead of every page change. 
    // To listen for when this page is active (for example, to refresh data), 
    // listen for the $ionicView.enter event: 
    // 
    //$scope.$on('$ionicView.enter', function(e) { 
    //}); 

    $scope.chats = Chats.all(); 
    $scope.remove = function(chat) { 
    Chats.remove(chat); 
    } 
}) 

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) { 
    $scope.chat = Chats.get($stateParams.chatId); 
}) 

.controller('AccountCtrl', function($scope) { 
    $scope.settings = { 
    enableFriends: true 
    }; 
}); 

和製表chats.html

<ion-view view-title="Chats"> 
    <ion-content> 
    <ion-list> 
     <ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}"> 
     <img ng-src="{{chat.face}}"> 
     <h2>{{chat.name}}</h2> 
     <p>{{chat.lastText}}</p> 
     <i class="icon ion-chevron-right icon-accessory"></i> 

     <ion-option-button class="button-assertive" ng-click="remove(chat)"> 
      Delete 
     </ion-option-button> 
     </ion-item> 
    </ion-list> 
    <a class="button icon icon-right ion-chevron-right" href="#/templates/page">Scientific Facts</a> 
    </ion-content> 
</ion-view> 

回答

2

因爲我想你沒有<ion-nav-view name="home"> </ion-nav-view>在代碼中的任何地方定義。

.state('page', { 
    url: '/templates/page', 
    templateUrl: 'templates/page.html', 
    controller: 'PageCtrl' 
    } 
} 

如果您已經定義了它。那麼它應該是

.state('page', { 
    url: '/templates/page', 
    views: { 
    'home': {  // it should be in quotes. 
     templateUrl: 'templates/page.html', 
     controller: 'PageCtrl' 
    } 
} 

我知道你是新的和所有的,但請首先通過DOCS。你需要完全理解路由才能做你想做的事。

0

我遇到過這個問題,可能很難找出白屏有什麼問題。

但是按照此檢查清單,你可能得到它解決:

1)在您的index.html,請檢查您是否不包括任何不存在的或錯誤的JS腳本。 (腳本src標籤)。

2)仔細檢查你的JS文件在你的index.html中加載時的順序。

3)刪除每個狀態URL配置結尾處的分號(;)。

4)請務必使用一個單引號,當你正在做的:

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

5)不要猶豫,註釋代碼在有關白色屏幕URL控制器。

6)最後,嘗試將您想要路由的URL替換爲另一個可以在測試應用程序時看到的URL。例如,如果在嘗試路由到/用戶時看到白屏,但您可以看到/ home視圖。用/ home替換$ urlRouterProvider.otherwise,看看它是否在真實設備或仿真器上測試時顯示。通過這樣做,您就可以知道它是包含可能錯誤的白屏路由的視圖或控制器。

0

此問題是由於語法錯誤或代碼中某處的邏輯錯誤造成的。您需要調試JavaScript代碼以找出它的位置。這裏是你如何做到這一點:

1)比方說,當你路由到/用戶的URL時,你會得到白色屏幕的死亡。

2)註釋與/用戶視圖相關的控制器的所有代碼。你應該能夠擺脫白屏。

3)取消註釋代碼並開始在控制器函數範圍中的每個代碼行之後添加一些alert()對話框,並搜索它在哪裏阻擋白屏。

例如,我得到了死亡的白屏,因爲在我看來,我正在對由我的一個函數控制器動態創建的數組進行ng-repeat,並且在獲取過程之前無法加載它完成。一旦我找到了這個問題,我開始使用ng-init加載數組,然後使用ng-repeat進行迭代。一切都已經修復!

正如我所說,你得到的白色屏幕更多地是在代碼中發生的邏輯錯誤。希望它有幫助。