4

I have dinamic tabs (Angular UI Bootstrap) in my view this way:

 <uib-tabset active="1" id="tabs" class="col-md-10">
            <uib-tab ng-repeat="tab in filaCtrl.tabs" ng-click="filaCtrl.getChatTab(tab.protocolo)">
                <uib-tab-heading >
                    <div style='display: flex; align-items: center; justify-content: center;'>
                        <h5 style='margin-right: 10px;'>Protocolo: {{tab.protocolo}}</h5>
                        <h7 ng-md-icon icon='cancel' style='fill:#F44336' size='16' ng-click='filaCtrl.closeTab(tab.protocolo, $index)'><h7>
                    <div>
                </uib-tab-heading>
            <div class="tab-content">
(...)

My function filaCtrl.closeTab() remove a tab, i.e., a item in array filaCtrl.tabs. But when a item is removed the view is updated and 'close' all tabs, i.e., refresh page.

self.closeTab = function (protocolo, $index) {
          self.tabs.splice($index, 1);
        };

How can i remove item without refresh my page?

2 Answers 2

1

ng-repeat does a two-way-binding so that objects changed in the view update the controller and vice-versa.

If you want to one-time-bind the the tabs array, you could define your ng-repeat statement as:

<uib-tab ng-repeat="tab in ::filaCtrl.tabs" ng-click="filaCtrl.getChatTab(tab.protocolo)">

Note that the :: syntax will one-time-bind your array.

Sign up to request clarification or add additional context in comments.

Comments

0

This is expected behavior in Angular (and the magic of data binding). You will need to make a copy of self.tabs and use that in your ng-repeat

self.tabsCopy = angular.copy(self.tabs);

Then you can do

<uib-tab ng-repeat="tab in filaCtrl.tabsCopy" ng-click="filaCtrl.getChatTab(tab.protocolo)">

However, note that your closeTab function uses the index of the tab in your array. So if the user closes multiple tabs, you will run into issues since your tabs and tabsCopy array will be different.

A better approach is to use some kind of variable like tab.open or tab.closed to track the state of a tab. Then you can do something like

<uib-tab ng-repeat="tab in filaCtrl.tabs" ng-click="filaCtrl.getChatTab(tab.protocolo)">
  <uib-tab-heading ng-if="tab.open>
    ...

1 Comment

Doesn't work. When valeu of tab.open changes refresh the page too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.