繁体   English   中英

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

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

我想在用户单击另一个选项卡时取消选项卡更改/面板切换,但我发现当前面板中的更改未保存。

我使用这里记录的元素<tab>deselect()属性在我的控制器中触发一个函数,并在其中确定我不应该更改选项卡。 含义:我不想取消选择它并选择用户单击的另一个。

我怎样才能做到这一点? 最好是从控制器内部,还是以任何其他方式?

我不能只做$event.stopPropagation()因为我这里没有$event ......我错过了什么?

隔离问题的Plunker

我防止选项卡更改的解决方案是使用内置的禁用标志。 如果向选项卡添加 ng-click 处理程序,您仍然可以对事件做出反应。 如果禁用的样式对您没有问题,这将起作用 - 在您的用例中可能是这种情况。

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

看看这个问题评论 这对我帮助很大。

请将此选项卡解决方案使用以下代码,它对我有用。

 //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 {}

假设您有 3 个选项卡并希望第三个选项卡不可点击。 然后你应该为可点击的选项卡添加一个deselect属性,这样当取消选择事件发生时,他们检查我们试图切换到哪个选项卡,如果它是第三个选项卡,则阻止选项卡切换。

这仅适用于 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();
    }
};

ng-click指令应用于每个选项卡:

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

然后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