Leveraging the AngularJS model within a tabset framework

I am working with a tabset that has two options and I am binding data from a JSON file using Angular. What I would like to do is log the name of the tab that I click on to the console. I thought about using a "model" for this, but I am not sure if that is the right approach.

Here is my tabset:

<tabset>
  <tab heading="tab1">
    <div class="panel-body">
        content
    </div>
  </tab>

   <tab heading="tab2">
    <div class="panel-body">
        content
    </div>
  </tab>
</tabset>

I have come across something like this before:

<div class="radio radio-text" ng-init="topTable.value='topCategory'">
    <label class="radio-inline"><input type="radio" ng-model="topTable.value" value="topCategory" ng-change="updateTotals(topTable.value)">TY Top 30 Categories</label>
    <label class="radio-inline"><input type="radio" ng-model="topTable.value" value="topSupplier" ng-change="updateTotals(topTable.value)">TY Top 10 Suppliers</label>
    <label class="radio-inline"><input type="radio" ng-model="topTable.value" value="topBrand" ng-change="updateTotals(topTable.value)">TY Top 10 Brands</label>
</div>

I would like to achieve something similar, where I can perform an action when a tab is clicked, just like with the radio buttons.

Answer №1

I'm uncertain about the exact location of the 'tabset' directive declaration. However, assuming you have the directive definition accessible:

app.directive('tab', function() {
   return {
       restrict : 'E',
       link : function(scope, element, attrs) {
           element.on('click', function() {
               console.log('This is tab: ', attrs.heading);
           }
       }

   }
});

and in your HTML:

<tabset>
  <tab heading="tab1">
    <div class="panenl-body">
      content
    </div>
  </tab>

  <tab heading="tab2">
    <div class="panenl-body">
      content
    </div>
  </tab>
</tabset>

By implementing this method, we are avoiding the use of ng-click. Since there is no update to any scope or controller object, there is no necessity to initiate a digest cycle using ng-click.

Answer №2

If you're looking to create a tabset, you can achieve it by using the following code snippet:

<tabset>
  <tab heading="tab1" ng-click="tabClicked('This is name 1')">
    <div class="panenl-body">
      content
    </div>
  </tab>

  <tab heading="tab2" ng-click="tabClicked('This is name 2')">
    <div class="panenl-body">
      content
    </div>
  </tab>
</tabset>

Remember to include the following in your controller:

$scope.tabClicked = function(value) {
  console.log(value);
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

After using `setAttribute`, React is unable to produce any audio

Currently, I am facing an issue with a React component where it should play sound from an array of IDs stored in the database by setting the ID to the src attribute for the source tag. However, this functionality is not working as expected. Interestingly, ...

What steps can be taken to customize this code in order to develop a dictation application?

Currently, I have a code that functions by comparing two strings: str2 (which represents user input) and str1 (our reference string). The purpose is to determine whether any words in str2 are spelled correctly or incorrectly. So far, the code works well. H ...

Utilizing API calls within a loop using AngularJS

I need to execute a series of APIs in a loop, but I want the second loop to start only after receiving the result from the last API call in the first loop. How can I achieve this? for(var i=0; i<array.length; i++ ) service.getfunction(array[i]).t ...

firebaseArray is returning a size of zero

Seeking assistance in determining the array length from Firebase, I have implemented the following code: (function() { 'use strict'; angular .module('com.td.oca.tDUITestHomepage') .controller('HomepageCont ...

What is the best way to include numerous optional parameters within a single route in Express?

Been a huge fan of Stackoverflow and finally decided to ask my first question here! I've been working on a JavaScript Express project, trying to figure out if it's possible to achieve the desired functionality under a single GET request. Struggli ...

Identify the key name in an array of objects and combine the corresponding values

Here is an example of my array structure: array = [{ "name": "obj0_property0", "url": "picture1" }, { "name": "obj1_property0", "url": "picture1" }, { "name": "obj0_property1", "url": "picture2" }] I am looking to transform this array using J ...

Reaching out to a particular individual with a message

In my coding dilemma, I am trying to make a bot send a message every minute to a particular user, not all users. The struggle is real! guild.members.cache.forEach(member => { setInterval(() => { member.send('hello').catch(error =&g ...

What are the steps involved in generating and implementing dynamic hierarchical JSON data structures?

I am currently creating a dynamic diagram using d3.js that incorporates hierarchical data. The goal is to make it interactive so that users can manipulate the hierarchy by adding or removing data values and children. I'm wondering if there is a way to ...

"Reposition all elements contained within a div that have a specific class

I'm completely new to Javascript and I've been trying to move all elements with a specific class inside a particular div. After doing some research, I found a solution that worked using IDs. However, when I tried to adapt it to work with classNam ...

Using Bootstrap, jQuery, and PHP to dynamically load input fields in a modal window

I am looking to dynamically load input fields in a modal window using PHP. While researching, I came across suggestions to utilize AJAX for this purpose. However, I have limited knowledge about AJAX. The plan is as follows: 1) When the user clicks on the ...

How can I load data from the server into CKEditor in React.js once the data is loaded?

Need to make changes in my code for editing an article item: const [contentEditor, setContentEditor] = useState(null); useEffect(() => { async function getLoadData(){ setitem(""); setContentEditor("" ...

Kartik's gridview in yii2 has a unique feature where the floating header in the thead and tbody are

I'm looking to create a table gridview with a floating header, where the tbody and thead are not the same. The image appears after refreshing the page, before the modal is refreshed. After refreshing the modal, this modal is inside pjax and it sets t ...

What is the best way to remove multiple rows from a table using Angular.js?

I'm attempting to tackle the task of deleting multiple rows from a table using checkboxes in Angular and PHP. Unfortunately, I've hit a roadblock and my understanding of Angular seems to have reached its limit! Let's take a look at my table ...

Determining the Size and Color of Squares in a Treemap Based on Content with D3.js

I encountered an issue with a treemap visualization. I came across an example that is similar to my situation, which can be viewed here: In the demo, you can see that the size of each square in the treemap is determined by the content size. However, all s ...

Extracting the URL of the @font-face declaration in CSS with the use of JavaScript

Currently, my CSS file contains numerous @font-face tags. Each tag specifies a unique font-family and src URL. @font-face{ font-family: Garamond; src: url('ePrintFonts/pcl_91545.ttf'); } @font-face{ font-family: C ...

Algorithm for File Naming

Given an array of desired file names in the order of their creation, where two files cannot have the same name. If a file has a duplicate name, it will be appended with (k), where k is the smallest positive integer that creates a unique name. Output an ar ...

"Exploring the world of jest.doMock: A guide to mocking

Here is the code snippet I am testing: ... import data from '../data/mock.json'; // function is async export const something = async () => { try { ... if (!data) { throw 'error is here!'; } ...

How to resolve a TypeError saying "Object(...) is not a function"?

I've been attempting to display material-ui tabs as a component on another page, but I'm encountering an error that causes the code to break when loading the page with this component. I've tried two different methods of rendering this compo ...

Interaction between index file and module instance

As I develop a computer system, I have divided it into various smaller components. My experience in software development has taught me the value of keeping systems compact and focused. To achieve this, I am creating modules that perform specific function ...

AngularJS: Transitioning from Expressions to Javascript (Coffeescript)

Looking to convert an expression into JavaScript in order to maintain an object's value in $scope: <dl class = "mortgage-information"> <dt><abbr title = "Loan-to-value Ratio">LTV</abbr></dt> <dd>{{(total_fi ...