[英]In angular bootstrap tabs, how to cancel tab change?
我防止選項卡更改的解決方案是使用內置的禁用標志。 如果向選項卡添加 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.