简体   繁体   English

ionic(AngularJS)中的ui-router的路由问题

[英]Routing issue with ui-router in ionic (AngularJS)

I have UI routes defined in ionic, and made a console.log in controller, but my problem is when I switch between URLs using ng-href="/#/LINK" then no data displayed in console, but when I refresh the same page I am able to see the data in console .... let me know what I am doing wrong here. 我在ionic中定义了UI路由,并在控制器中创建了console.log ,但是我的问题是,当我使用ng-href =“ /#/ LINK”在URL之间切换时,控制台中没有显示任何数据,但是当我刷新相同的内容时页面,我能够在控制台中查看数据..让我知道我在这里做错了。

Routes - 路线-

  .state('app.roomone', {
    url: "/roomone/:id",
    views: {
      'menuContent': {
        templateUrl: "templates/roomone.html",
        controller: 'RoomoneCtrl'
      }
    }
  })

  .state('app.roomtwo', {
    url: "/selecroom/:roomname/:roomindex",
    views: {
      'menuContent': {
        templateUrl: "templates/roomtwo.html",
        controller: 'RoomtwoCtrl'
      }
    }
  })

Following are my controllers - 以下是我的控制器-

.controller('RoomoneCtrl', function($scope, $rootScope) {
  console.log("I am in room one");
});

.controller('RoomtwoCtrl', function($scope, $rootScope) {
  console.log("I am in room two");
});

In my template - I am having simple -- <a ng-href="/#/roomone/MY_ID" />Dummy Link</a> 在我的模板中-我很简单- <a ng-href="/#/roomone/MY_ID" />Dummy Link</a>

Problem - Only on refresh(F5) I am getting console values like ""I am in room one"" not on simple navigation of link. 问题-仅在刷新(F5)时,我才获得控制台值,例如““我在一个房间里”“,而不是在链接的简单导航上。 Let me know what I am doing wrong. 让我知道我在做什么错。

As pointed out in the comments: the view is cashed and therefore the controller is not initialized the second time you visit the page. 如评论中所指出的:该视图是现金视图,因此第二次访问该页面时不会初始化控制器。

One solution was not mentioned in Template does not update when using ui-router and ion-tabs . 在使用ui-router和ion-tabs时模板不会更新中未提及的一种解决方案。 (change cashing to false in view or in router) therefor I adding an extra answer here (better was adding it to comments but with the code example this would become unclear) (将视图或路由器中的兑现更改为false),为此我在此处添加了一个额外的答案(最好将其添加到注释中,但是使用代码示例将变得不清楚)

Listen in your controller (or directive) to the $ionicView.beforeEnter event to know when a view is active. 在控制器(或指令)中侦听$ ionicView.beforeEnter事件,以了解视图何时处于活动状态。

.controller('RoomoneCtrl', function($scope, $rootScope) {
    $scope.$on('$ionicView.beforeEnter', function () {
        // (on first time added to DOM and after the view becomes active after cached
        // change some value on your scope
        $scope.date = new Date();
        console.log("I am in room one");
    });
});

For ionic view events see: http://ionicframework.com/docs/api/directive/ionView/ 有关离子视图事件,请参见: http : //ionicframework.com/docs/api/directive/ionView/

I am also faced the same issue. 我也面临同样的问题。 This is a cache issue. 这是一个缓存问题。 I have fixed the issue by using cache : false .You have have add it in your route as the below, 我已经通过使用cache修复了这个问题:false 。您已经按照以下方法在路由中添加了它,

.state('app.roomone', {
    url: "/roomone/:id",
    cache : false,
    views: {
      'menuContent': {
        templateUrl: "templates/roomone.html",
        controller: 'RoomoneCtrl'
      }
    }
  })

  .state('app.roomtwo', {
    url: "/selecroom/:roomname/:roomindex",
    cache : false,
    views: {
      'menuContent': {
        templateUrl: "templates/roomtwo.html",
        controller: 'RoomtwoCtrl'
      }
    }
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM