离子:路由给出一个空白页面

时间:2014-05-16 05:21:26

标签: angularjs angular-ui-router ionic-framework

我刚刚开始学习角度和离子来构建应用程序。

我刚刚启动了一个包含ionic的新应用程序,并从json文件中创建了一个项目列表。这很完美,但是因为即时跳转到路由我只看到一个空白页面,我不会弄错。

这是我的index.html:

<!DOCTYPE html>
<html >
    <head>
        <meta charset="utf-8">
        <title>Spätifinder</title>
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <link rel="stylesheet" href="css/ionic.css">
        <script src="angular.min.js"></script>
    <script src="js/ionic.bundle.js"></script>
    <script src="app.js"></script>
    </head>
    <body ng-app="spaetifinder">
        <ion-header-bar type="bar-positive" 
             animation="nav-title-slide-ios7" 
             back-button-type="button-icon" 
             back-button-icon="ion-ios7-arrow-back"></ion-header-bar>

    <ion-nav-bar class="bar-energized nav-title-slide-ios7">
  </ion-nav-bar>
  <ion-nav-view></ion-nav-view>
    </body>
</html>

这是ma app.js文件:

var app = angular.module('spaetifinder', ['ionic']);

app.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

            .state('home', {
                url: '/',
                templateUrl: 'home.html'
            });

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

    });

app.controller('StoreController', [ '$http', function($http) {

        var store = this;

        store.storeList = [ ];

        $http.get('http://m.xn--sptifinder-r5a.de/apiv1/jsonp.php?action=list&lat=52.437595&long=12.987900&distance=100').success(function(data){
            store.storeList = data;
        });

        this.loadImage = function(hasImage){
            if(hasImage = 0) {
                return "http://www.spätifinder.de/images/fotos/no-foto.jpg";
            }
            else {
                return this.ID;
            }
        };
    }]);

这应该是我的家庭模板(home.html)

<!-- our list and list items -->
        <ion-list>
          <a href="#" class="item item-thumbnail-left" ng-repeat="spaeti in spaetis.storeList">
            <img ng-if="spaeti.has_image == 0" ng-src="http://www.spätifinder.de/images/fotos/no-foto.jpg">
            <img ng-if="spaeti.has_image == 1" ng-src="http://www.spätifinder.de/images/fotos/{{spaeti.ID}}_crop.jpg">
            <h2>{{spaeti.Name}}</h2>
            <p>{{spaeti.BusinessHours}}<br>{{spaeti.Street}}, {{spaeti.ZIP}} {{spaeti.City}}</p>
          </a>
        </ion-list>

我只是不知道这有什么问题,也许你在那里看到了错误?

提前致谢

LUC

5 个答案:

答案 0 :(得分:2)

我认为您的问题出在路由配置中。您只能在/网址上定义一个州。然后,您将回退设置为不存在的/home。您可能希望自己的家乡位于/home

答案 1 :(得分:1)

编辑:抓住我认为我看到了你的问题,你在州街区尽头有一个;,我不得不删除我的工作,我昨晚花了很长时间才弄明白我们希望它对你有帮助。

旧答案:

我不确定这可能会有所帮助,但这是我的,我唯一看错的是@ Jon7已经指出的。

.state('main', {
        url: "/main",
        templateUrl: "templates/main.html",
        controller: 'MainController'
    })

$urlRouterProvider.otherwise('/main');

答案 2 :(得分:0)

我也很难理解路由。我从这个Codepen示例中学到了很多东西。

Codepen example routing

答案 3 :(得分:0)

你需要添加控制器,也许还需要一个视图......

所以

.state('home', {
  url: '/',
  templateUrl: 'home.html'
});

.state('home', {
  url: '/',
  views: {    //optional 
    'viewname':{
        templateUrl: 'templates/home.html'
        controller: 'StoreController' 
     }
    }
 });

答案 4 :(得分:0)

你的错误就是其他声明。它应该是:

$urlRouterProvider.otherwise('/')

var app = angular.module('ionicApp', ['ionic'])

app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/')

    $stateProvider.state('home', {
        url: '/',
        template: '<p>Hello, world!</p>'
    })
})

$stateProvider.state('home', {
    url: '/',
    templateUrl: 'home.html'
})

http://learn.ionicframework.com/formulas/navigation-and-routing-part-1/

相关问题