2015-07-12 103 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进行迭代。一切都已经修复!

正如我所说,你得到的白色屏幕更多地是在代码中发生的逻辑错误。希望它有帮助。