Implementing angularJs factory or service for bootstrap tabs

how to write a service or factory for bootstrap tabs using angularJs. I fit in one controller, but for different controllers you need a common function (re-code).

<ul class="nav nav-tabs">
    <li data-ng-class="{active:tab===0}">
        <a href ng-click="changeTab(0)"> <i class="fa fa-list"></i></a>
    </li>
    <li data-ng-class="{active:tab===1}">
        <a href ng-click="changeTab(1)"><i class="fa fa-user"></i></a>
    </li>
</ul>

<div data-ng-class="{activeTab:tab===0}" ng-show="isActiveTab(0)">
  tab1
</div>
<div data-ng-class="{activeTab:tab===1}" ng-show="isActiveTab(1)">
  tab2
</div>

      

controller

$scope.tab = 0;    
    $scope.changeTab = function(newTab){
      $scope.tab = newTab;
    };    
    $scope.isActiveTab = function(tab){
      return $scope.tab === tab;
    }; 

      

+1


source to share


3 answers


Hi i am writing it without using any service or factory see example

<ul ng-init="tab = 1">          
    <li ng-class="{active:tab===1}"> 
      <a href ng-click="tab = 1">Tab1</a>               
    </li>           
    <li ng-class="{active:tab===2}"> 
       <a href ng-click="tab = 2">Tab2</a>              
    </li>           
    <li ng-class="{active:tab===3}"> 
      <a href ng-click="tab = 3">Tab3</a>           
    </li>           
    <br><br> 
    <p ng-show="tab === 1"> Tab1 content </p>           
    <p ng-show="tab === 2"> Tab2 content</p> 
    <p ng-show="tab === 3"> Tab3 content</p>        
</ul>

      



Change it dynamically via controller, see working example here

+1


source


Tab management is a problem. Instead of a factory, it is recommended to create two directives: tabContainer

and tab

. The directive is tab

registered with the parent tabContainer

using the attribute require

to access the parent directive controller API.

Demo

Using

<tab-container selected="tab2">
   <tab name="tab1">tab1</tab>
   <tab name="tab2">tab2</tab>
</tab-container>

      

Parent directive

The parent directive publishes the following controller API, which will be accessed by the child tab directives:

tabContainer controller

// registers this tab with the parent tabContainer
this.register = function(element) {
  $scope.tabs.push(element);
}
// returns the selected tab object whose 
// name property indicates which tab is active
this.getSelected = function() {
  return $scope.selectedTab;
}

      

Directive for children

The tab directive can refer to the parent controller by requiring the parent directive in its directive definition and by referring to the parent directive controller as the 4th argument of the tab pointer reference function:

defining a tab stop

scope: true,
require: '^tabContainer',
link: function(scope, element, attr, tabContainer) {
  // set the tab so that it is visible in the tab directive scope.
  scope.tab = { name: attr.name, element:element};
  scope.selectedTab = tabContainer.getSelected();
  tabContainer.register(scope.tab);

}

      

The volume is equal true

so that each tab creates its own child area and does not interfere with the area of ​​other tabs.

Template files



For example, directive templates are embedded in HTML:

<script type="text/ng-template" id="tabContainer.html">
  <ul class="nav nav-tabs">
    <li ng-repeat="tab in tabs" data-ng-class="{active:selectedTab.name===tab.name}">
      <a href ng-click="changeTab(tab)"> <i class="fa fa-list">{{tab.name}}</i></a>
    </li>
  </ul>
  <ng-transclude></ng-transclude>
</script>
<script type="text/ng-template" id="tab.html">
 <div data-ng-class="{activeTab:selectedTab.name===tab.name}" ng-show="selectedTab.name === tab.name">
     <ng-transclude></ng-transclude>
 </div>
</script>

      

It is recommended to move them to dedicated HTML files.

Change active tab

The user can change the active tab by clicking the tab link. This is achieved by posting the function $scope

to the parent controller:

$scope.changeTab = function(tab) {
  $scope.selectedTab.name = tab.name;
}

      

Creating a tab module

The beauty of AngularJS and its pluggable modular architecture is that you can extend the AngularJS ecosystem system and keep directives running smoothly. For example, you can encapsulate the above tab directive into a module, tabs

and even use the ngRepeat directive to bind tabs.

Demo

controller

var app = angular.module('app',['tabs']);
app.controller('ctrl', function($scope) {
   $scope.tabData = [ 
      { name: 'tab1', body: 'You selected tab1!'},
      { name: 'tab2', body: 'You selected tab2!'},
      { name: 'tab3', body: 'You selected tab3!'},
      { name: 'tab4', body: 'You selected tab4!'},
    ];
});

      

View

<div class="container" ng-controller="ctrl">
   <tab-container selected="tab1">
     <tab ng-repeat="tab in tabData" name="{{tab.name}}">{{ tab.body }} </tab>
  </tab-container>
</div>

      

+4


source


The structure of controllers / services in Angular is well explained here: https://github.com/johnpapa/angular-styleguide

In app.js we declare the Angular app, give it a name and any dependencies (ng-route / ng-grid). $ http calls must be made with a factory or service and the controller must call the service to retrieve or send data. From the Angular documentation, "services are replaceable objects that are linked together using Dependency Injection (DI). You can use services to organize and share code in your application." https://docs.angularjs.org/guide/services

app.js:

var app = angular.module('appname', []);

app.factory('httpResponseErrorInterceptor'...
app.config(['$httpProvider',...

      

Controller:

    angular.module('appname').controller("NameCtrl", ["$scope", "$log", "$window", "$http", "$timeout", "SomeService",
    function ($scope, $log, $window, $http, $timeout, TabService) {

    //your controller code
       $scope.tab = 0;    
       $scope.changeTab = function(newTab){
          $scope.tab = newTab;
       };    
       $scope.isActiveTab = function(tab){
          return $scope.tab === tab;
       }; 
    }
]);

      

Services:

angular.module('appname').service("TabService", function () {
   //Your code for shared functionality regarding tab service

   var currentTab = {};

   return {
      currentTab : currentTab 
   }
});

      

+1


source







All Articles