简体   繁体   English

Ion-View不刷新(Angular UI-Router)

[英]Ion-View does not refresh (Angular UI-Router)

I have this classic ionic view refresh problem: 我有这个经典的离子视图刷新问题:

.state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  })
   .state('app.menu', {
    url: "/menu",
    views: {
      'menuContent': {
        templateUrl: "templates/headmenu.html"
      }
    }
  })
  .state('app.menu.media', {
    url: "/media/:MediaType",
    views: {
      'media-tab': {
        templateUrl: "templates/tabs/media.html",
        controller: "MediaCtrl"
      }
    }
  }).state('app.settings', {
    url: "/settings",
    views: {
      'settingsContent': {
        templateUrl: "templates/settings.html",
        controller: "SettingsCtrl"
      }
    }
  })

The app start from 'app.menu.media' state, I click on 'settings', the view is loaded, but the problem occurs when I try going back to 'app.menu.media' state, the view is not refreshed (I still have the 'settings' view) 该应用程序从'app.menu.media'状态开始,我点击'设置',视图已加载,但当我尝试返回'app.menu.media'状态时,问题就出现了,视图没有刷新(我仍然有'设置'视图)

One of the solutions suggested by other devs is using ui-sref-opts="{reload: true, notify: true}" inside a <a ui-sref="app.menu.media" ...> ..</a> tag, but it's not smooth, the users feel like if the app freezes for a short time, and the same goes for the javascript solution: $state.go('app.menu.media', {}, {reload: true}); 其他开发人员建议的解决方案之一是在<a ui-sref="app.menu.media" ...> ..</a>使用ui-sref-opts="{reload: true, notify: true}" <a ui-sref="app.menu.media" ...> ..</a>标签,但它不流畅,用户觉得如果应用程序冻结了一小段时间,javascript解决方案也是如此: $state.go('app.menu.media', {}, {reload: true});

I know that the controller is not loaded when it is in the same state, but in this example we have different controllers: "MediaCtrl" and "SettingsCtrl", so why we have this problem? 我知道控制器在处于相同状态时没有加载,但在这个例子中我们有不同的控制器:“MediaCtrl”和“SettingsCtrl”,那么为什么我们有这个问题呢? Is there any clean solution to fix this problem? 有没有解决这个问题的干净解决方案?

Edit: This problem occurs even if I'm disabling the cache: $ionicConfigProvider.views.maxCache(0); 编辑:即使我禁用缓存,也会出现此问题: $ionicConfigProvider.views.maxCache(0);

After Debuging the app, I found that the problem was not related to cache, but it doesn't mean that enabling the views cache will not affect the app, disabling the cache was important for my case. 在Debuging应用程序之后,我发现问题与缓存无关,但这并不意味着启用视图缓存不会影响应用程序,禁用缓存对我的情况很重要。

So the problem was related to the fact that I was using the same state " app " for both ' media ' and ' settings ' views, with different view names: " settingsContent " for "app.settings" and "menuContent" for "app.menu". 所以,问题是有关的事实,我用的是相同的状态“ 应用 ”两个“ 媒体 ”和“ 设置 ”的观点,不同的视图名称:为“应用程序“settingsContent”为“app.settings”和“menuContent” 。菜单”。 So I had to add these two Ion-nav-views to the template Url linked to the " app " state, which is " menu.html ", juste like this: 所以我不得不将这两个离子导航视图添加到链接到“ app ”状态的模板Url,这是“ menu.html ”,这样的juste:

 <ion-nav-view name="menuContent"></ion-nav-view>
 <ion-nav-view name="settingsContent"></ion-nav-view>

So when I go back to " app.menu.media " state, the view is indeed loaded, but the " settings " view is still there, and it is hiding my new requested view " media "! 因此,当我回到“ app.menu.media ”状态时,视图确实已加载,但“ 设置 ”视图仍然存在,并且它隐藏了我新请求的视图“ 媒体 ”!

So what I did to fix this issue is to use the same Ion View name: 所以我解决这个问题的方法是使用相同的Ion View名称:

.state('app.menu', {
url: "/menu",
views: {
  'THESAMENAME': {
    templateUrl: "templates/headmenu.html"
  }
}...
....
.state('app.settings', {
url: "/settings",
views: {
  'THESAMENAME': {
    templateUrl: "templates/settings.html",
    controller: "SettingsCtrl"
  }
}

And inside menu.html, I deleted the two previous lines, and replaced theme with: 在menu.html中,我删除了前两行,并用以下内容替换了主题:

<ion-nav-view name="THESAMENAME"></ion-nav-view>

I hope this could help someone 我希望这可以帮助别人

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

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