angular-ui-bootstrap,去掉最后一个tab,整个页面都刷新了
angular-ui-bootstrap, remove the last tab, the whole page was refreshed
我添加了 ng-click 事件来删除 uib-tab-heading.Everything 中的选项卡是可以的,但是当我删除最后一个选项卡时,整个页面都刷新了,这不是预期的。
这是我的代码:http://embed.plnkr.co/f0p9uZgKuWTNh3Ss7okY.
html
<head>
<link data-require="bootstrap-css@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css" />
<script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng-controller="TabsDemoCtrl">
<uib-tabset active="active">
<uib-tab index="0" heading="Static title">Static content</uib-tab>
<uib-tab index="$index+1" ng-repeat="tab in tabs track by $index">
<uib-tab-heading>
{{tab.title}}
<div class="close" aria-label="Close" ng-click="remove($index)">
<span aria-hidden="true">×</span>
</div>
<!--<div style="width: 10px; height: 10px; background-color: red;" ng-click="removeTab($index)"></div>-->
</uib-tab-heading>
{{tab.content}}
</uib-tab>
</uib-tabset>
</div>
</body>
</html>
js
angular.module('app', ['ui.bootstrap']).controller('TabsDemoCtrl', function ($scope, $window) {
$scope.tabs = [{
title: 'Dynamic Title 1',
content: 'Dynamic content 1'
},
{
title: 'Dynamic Title 2',
content: 'Dynamic content 2',
},
{
title: 'Dynamic Title 3',
content: 'Dynamic content 3',
},
{
title: 'Dynamic Title 4',
content: 'Dynamic content 4',
}
];
$scope.remove = function (index) {
$scope.tabs.splice(index, 1);
$scope.active = $scope.tabs.length;
};
});
感谢您的帮助。
<uib-tab-heading>
本身被 <a>
标签包裹,因此页面似乎是由于默认的 link 单击操作而刷新的。
做
<div ng-click="remove($index, $event)">
$scope.remove = function(index, event){
event.preventDefault();
...
}
您需要使用 ng-click 提供的 $event 对象,如文档所述:https://docs.angularjs.org/api/ng/directive/ngClick
html:
ng-click="remove($index, $event)"
js
$scope.remove = function (index, e) {
$scope.tabs.splice(index, 1);
$scope.active = $scope.tabs.length;
e.preventDefault();
};
这是您的代码的工作示例:https://plnkr.co/edit/QXzi1pZb4njydXMo01QA?p=preview
我添加了 ng-click 事件来删除 uib-tab-heading.Everything 中的选项卡是可以的,但是当我删除最后一个选项卡时,整个页面都刷新了,这不是预期的。 这是我的代码:http://embed.plnkr.co/f0p9uZgKuWTNh3Ss7okY.
html
<head>
<link data-require="bootstrap-css@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css" />
<script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng-controller="TabsDemoCtrl">
<uib-tabset active="active">
<uib-tab index="0" heading="Static title">Static content</uib-tab>
<uib-tab index="$index+1" ng-repeat="tab in tabs track by $index">
<uib-tab-heading>
{{tab.title}}
<div class="close" aria-label="Close" ng-click="remove($index)">
<span aria-hidden="true">×</span>
</div>
<!--<div style="width: 10px; height: 10px; background-color: red;" ng-click="removeTab($index)"></div>-->
</uib-tab-heading>
{{tab.content}}
</uib-tab>
</uib-tabset>
</div>
</body>
</html>
js
angular.module('app', ['ui.bootstrap']).controller('TabsDemoCtrl', function ($scope, $window) {
$scope.tabs = [{
title: 'Dynamic Title 1',
content: 'Dynamic content 1'
},
{
title: 'Dynamic Title 2',
content: 'Dynamic content 2',
},
{
title: 'Dynamic Title 3',
content: 'Dynamic content 3',
},
{
title: 'Dynamic Title 4',
content: 'Dynamic content 4',
}
];
$scope.remove = function (index) {
$scope.tabs.splice(index, 1);
$scope.active = $scope.tabs.length;
};
});
感谢您的帮助。
<uib-tab-heading>
本身被 <a>
标签包裹,因此页面似乎是由于默认的 link 单击操作而刷新的。
做
<div ng-click="remove($index, $event)">
$scope.remove = function(index, event){
event.preventDefault();
...
}
您需要使用 ng-click 提供的 $event 对象,如文档所述:https://docs.angularjs.org/api/ng/directive/ngClick
html:
ng-click="remove($index, $event)"
js
$scope.remove = function (index, e) {
$scope.tabs.splice(index, 1);
$scope.active = $scope.tabs.length;
e.preventDefault();
};
这是您的代码的工作示例:https://plnkr.co/edit/QXzi1pZb4njydXMo01QA?p=preview