简体   繁体   English

在角度引导选项卡中,如何取消选项卡更改?

[英]In angular bootstrap tabs, how to cancel tab change?

I want to cancel tab change/panel switch when user clicks another tab and I discover that changes in the current panel are not saved.我想在用户单击另一个选项卡时取消选项卡更改/面板切换,但我发现当前面板中的更改未保存。

I use the deselect() attribute of element <tab> , documented here , to fire a function in my controller and in which I determine that I shouldn't change tab.我使用这里记录的元素<tab>deselect()属性在我的控制器中触发一个函数,并在其中确定我不应该更改选项卡。 Meaning: I don't want to deselect this and select the other that user clicked on.含义:我不想取消选择它并选择用户单击的另一个。

How can I accomplish this?我怎样才能做到这一点? preferably from inside the controller, or in any other way?最好是从控制器内部,还是以任何其他方式?

I can't just do $event.stopPropagation() since I don't have a $event here... what am I missing?我不能只做$event.stopPropagation()因为我这里没有$event ......我错过了什么?

Plunker isolating the problem.隔离问题的Plunker

My solution to prevent the tab change was using the built in disabled flag.我防止选项卡更改的解决方案是使用内置的禁用标志。 If you add an ng-click handler to the tab, you can still react to events.如果向选项卡添加 ng-click 处理程序,您仍然可以对事件做出反应。 This works if the disabled styling is no problem for you - in your use case this might be the case.如果禁用的样式对您没有问题,这将起作用 - 在您的用例中可能是这种情况。

<tab  disabled="true" ng-click="warnUserNotSavedChanges()">

Take a look at this issue comment .看看这个问题评论 It helped me a lot.这对我帮助很大。

Please use below code for this tabs solution, It's worked for me.请将此选项卡解决方案使用以下代码,它对我有用。

 //our root app component import {Component, NgModule, VERSION} from '@angular/core' import {BrowserModule} from '@angular/platform-browser' import { TabsModule } from 'ngx-bootstrap'; @Component({ selector: 'my-app', template: ` <tabset> <tab heading='Tab 1'>Tab 1 content</tab> <tab> <template tabHeading> <div (click)="tryToSwitch($event)"> Tab 2 </div> </template> Tab 2 content </tab> </tabset> `, }) export class App { tryToSwitch(e) { let result = confirm('Are you sure?'); if(!result) { e.stopPropagation(); } } } @NgModule({ imports: [ BrowserModule, TabsModule.forRoot() ], declarations: [ App ], bootstrap: [ App ] }) export class AppModule {}

Suppose that you have 3 tabs and want the third tab to be unclickable.假设您有 3 个选项卡并希望第三个选项卡不可点击。 Then you should add a deselect attribute to clickable tabs, so that when deselect event happens, they checked what tab we're trying to switch to and if it's the third tab, prevented tab switch.然后你应该为可点击的选项卡添加一个deselect属性,这样当取消选择事件发生时,他们检查我们试图切换到哪个选项卡,如果它是第三个选项卡,则阻止选项卡切换。

This works only in later versions of ui-bootstrap, around 0.12-0.14, can't say exactly:这仅适用于 ui-bootstrap 的更高版本,大约 0.12-0.14,不能确切地说:

template.html

<uib-tabset class="tab-animation" active="activeTab">
    <uib-tab index="0" heading="Overview" id="overview" deselect="checkTab($event, $selectedIndex)"></uib-tab>
    <uib-tab index="1" heading="Details" id="details" deselect="checkTab($event, $selectedIndex)"></uib-tab>
    <uib-tab index="2" heading="Download" id="download"></uib-tab>
</uib-tabset>

controller.js

// Downloads tab shouldn't be clickable
$scope.checkTab = function($event, $selectedIndex) {
    // $selectIndex is index of the tab, you're switching to
    if ($selectedIndex == 2) { // last tab is non-switchable
        $event.preventDefault();
    }
};

Apply ng-click directive to each tab:ng-click指令应用于每个选项卡:

<tab ng-click="checkChange($event)">

and then preventDefault();然后preventDefault(); :

$scope.checkChange = function($e) {
    // check if tab can be changed
    $e.preventDefault();
};

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

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