简体   繁体   English

Angular Ui-Router-Extras | 试图实现粘性状态,但不起作用

[英]Angular Ui-Router-Extras | Trying to implement sticky states, but not working

Well, I have this plunkr trying to simulate my situation: 好吧,我有这个plunkr试图模拟我的情况:

I have 3 tabs and the idea is that the user type a word on the textbox, and when click the button, an angular service returns an answer (result) from a DB according to the typed on the textbox (I have simulated this process with requesting data to a json file, so it is not important whatever you type, always will return the whole data) and populated a table in another view. 我有3个选项卡 ,其想法是用户在文本框中键入一个单词,单击该按钮时, 角度服务会根据文本框中的类型从DB返回答案(结果)(我已使用请求数据到json文件,因此无论你键入什么都不重要,总是会返回整个数据)并在另一个视图中填充一个表。

What's my goal? 我的目标是什么? Well, I need to preserve every view intacted per tab. 好吧,我需要保留每个选项卡上的每个视图。 In other words, when the user click on a tab (changing the states) and returns to another, the data that was requested, could be there (and also what was typed on the search box). 换句话说,当用户单击选项卡(更改状态)并返回到另一个选项卡时,请求的数据可能存在(以及搜索框中键入的内容)。

EDIT: 编辑:

I have an array containing my definitions: 我有一个包含我的定义的数组:

var parentStates = [
      {state : 'tab1', url: '/tab1/', template: 'tab1.html', s: false, d: true},
      {state : 'tab2', url: '/tab2/', template: 'tab2.html', s: false, d: true},
      {state : 'tab3', url: '/tab3/', template: 'tab3.html', s: false, d: true},
      {state : 'tab1.table', url: '', template: 'table.html', s: true, d: false},
      {state : 'tab2.table', url: '', template: 'table.html', s: true, d: false},
      {state : 'tab3.table', url: '', template: 'table.html', s: true, d: false}
    ];

And I define my states dinamycally, in this way: 我以这种方式定义我的状态dinamycally:

parentStates.forEach(function(childName){
        $stateProvider
          .state("home." + childName.state, {
            controller: "itemController", resolve: { $title: function() { return childName.state; } },
            url: childName.url,
            templateUrl: childName.template,
            sticky: childName.s,
            deepStateRedirect: childName.d
          })
      });

Applying only sticky to the states home.tab*.table . 仅将粘性应用于状态home.tab*.table This works, but not as espected. 这有效,但不是那么受人尊敬。 For example, what I type on search box, dessapear when I change the tab. 例如,我在搜索框中键入的内容,当我更改选项卡时dessapear。 I need that the content from the 3 arrays (mentioned below) could bee keeped per tab. 我需要每个标签可以保留3个数组(如下所述)的内容。 That's my issue. 那是我的问题。

I have 3 arrays: 我有3个数组:

$scope.Model.items;
$scope.Model.filteredlist;
$scope.Model.expenses;

and I use a function called $scope.clear_arrays to clear the data on the arrays (even on the $stateChangeSuccess ). 我使用一个名为$scope.clear_arrays的函数来清除数组上的数据(甚至在$stateChangeSuccess )。 I don't know if this is ok to get what I want. 我不知道这是否可以得到我想要的东西。

I know I have to use the great angular-ui-router-extras (sticky states) library, but I don't know how to apply it in my case. 我知道我必须使用伟大的angular-ui-router-extras (sticky states)库,但我不知道如何在我的情况下应用它。 I have tried with: 我尝试过:

sticky: true,
deepStateRedirect: true,

but does not works. 但不起作用。 Some ideas, some help? 一些想法,一些帮助?

EDIT: 编辑:

As the docs says, I need exactly this part: 正如文档所说,我需要这一部分:

... The states for a tab in the application does not get exited when switching to one of the other tabs. ...当切换到其他选项卡之一时,应用程序中的选项卡的状态不会退出。 The user should be able to switch back and forth between tabs without any interruption in their workflow. 用户应该能够在标签之间来回切换,而不会中断他们的工作流程。

There are a few design decisions to keep in mind when using ui-router-extra's sticky states: 使用ui-router-extra的粘性状态时,需要记住一些设计决策:

  1. Use a named view for each state, and hide/show them as these states become active. 为每个状态使用命名视图,并在这些状态变为活动状态时隐藏/显示它们。 This makes if you think about it... if we remove those views from the DOM entirely then the state of that part of your app is gone (think of a partially typed input box for example). 这使得如果你考虑一下......如果我们完全从DOM中删除那些视图,那么你的应用程序部分的状态就会消失(例如,想想部分类型的输入框)。 Here's an example: 这是一个例子:

    <div ng-show="$state.includes('main.tab1')" ui-view="tab1"></div>

  2. Use a shared controller for your tabs. 为选项卡使用共享控制器。 Because of the nature of sticky states, in order to share a state, it's best (and thus far the only way I've found) to make this work. 由于粘性状态的性质,为了共享一个状态,最好(也是迄今为止我找到的唯一方法)才能完成这项工作。 This step is quite simple, just don't assign a controller to your sub-states and you should be good to go. 这个步骤非常简单,只是不要将控制器分配给子状态,你应该好好去。

Below is a simple state definition taking advantage of the steps described above, including a parent route, controller-less child, and sticky states: 下面是一个利用上述步骤的简单状态定义,包括父路由,无控制器子和粘滞状态:

$stateProvider
.state('main', {
  url: '/',
  controller: 'MainCtrl',
  controllerAs: 'main',
  templateUrl: 'state-main.html'
})
.state('main.tab1', {
  url: '/tab1',
  sticky: true,
  views: {
    tab1: {
      templateUrl: 'state-tab1.html',
    }
  }
})

Here is a very simplistic example using sticky states: http://plnkr.co/edit/SCHExh4DYKFd9Kq3UbaA 以下是使用粘性状态的非常简单的示例: http//plnkr.co/edit/SCHExh4DYKFd9Kq3UbaA

For your app, I applied the principles laid out above and removed the table sub-states as those were complicating things a bit. 对于你的应用程序,我应用了上面列出的原则并删除了表子状态,因为这些使事情变得复杂。 So to simplify I took the liberty to wrap the table in a directive and passed the data into it from the parent scope. 因此,为了简化,我冒昧地将表包装在一个指令中,并将数据从父作用域传递给它。

The usage of that directive became pretty simple (feel free to rename it more appropriately): 该指令的使用变得非常简单(可以更自由地重命名它):

<table-list ng-if="Model.tab1.length > 0" list="Model.tab1" />

Keep in mind that Model.tab1 contains the array of elements for the tab1 state (remember shared controller). 请记住, Model.tab1包含tab1状态的元素数组(记住共享控制器)。

Finally, here is your app working with sticky states: http://plnkr.co/edit/gLn19DMyKBTwZRMpv6Fo 最后,这是您的应用程序使用粘性状态: http//plnkr.co/edit/gLn19DMyKBTwZRMpv6Fo

I hope this helps :) 我希望这有帮助 :)

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

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