离子(AngularJS)路由问题与UI路由器 [英] Routing issue with ui-router in ionic (AngularJS)

查看:108
本文介绍了离子(AngularJS)路由问题与UI路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在离子定义UI的路线,并在控制器做了一个的console.log ,但我的问题是,当我使用NG-HREF =/#的URL之间切换/ LINK,然后在控制台中显示没有数据,但是当我刷新同一页,我能够看到控制台的数据提供....让我知道我在做什么错在这里。

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.

路线 -

  .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");
});

在我的模板 - 我有简单 - <一个NG-HREF =/#/ roomone /添加my_id/>虚拟链接< / A>

In my template - I am having simple -- <a ng-href="/#/roomone/MY_ID" />Dummy Link</a>

问题 - 只有在刷新(F5)我越来越控制台值,如我一个房间,不链接简单的导航。让我知道我做错了。

Problem - Only on refresh(F5) I am getting console values like ""I am in room one"" not on simple navigation of link. 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.

的一个解决方案未在<一个提到href=\"http://stackoverflow.com/questions/28827928/template-does-not-update-when-using-ui-router-and-ion-tabs\">Template使用UI路由器和离子标签时不更新。 (改变兑现为false视图或路由器)为此我在这里增加一个额外的答案(更好的是它添加到评论,但与code例子,这会变得不清晰)

One solution was not mentioned in Template does not update when using ui-router and 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)

听在你的控制器(或指令)到$ ionicView.beforeEnter事件知道,当一个看法是积极的。

Listen in your controller (or directive) to the $ionicView.beforeEnter event to know when a view is active.

.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");
    });
});

有关离子电视事件,请参阅: http://ionicframework.com/docs/api/directive / ionView /

For ionic view events see: http://ionicframework.com/docs/api/directive/ionView/

这篇关于离子(AngularJS)路由问题与UI路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆