ng-repeat not functioning properly with custom tabs

Everything was working perfectly until I added ng-repeat to the content

<ul>          
    <li ng-class="{active:tab===1}"> 
      <a href ng-click="tab = tab==1 ? a : 1">Tab1</a>               
    </li>           
    <li ng-class="{active:tab===2}"> 
       <a href ng-click="tab = tab==2 ? a : 2">Tab2</a>              
    </li>           
    <li ng-class="{active:tab===3}"> 
      <a href ng-click="tab = tab==3 ? a : 3">Tab3</a>           
    </li>     
       </ul>      
    <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>        

I then successfully added ng-repeat only in the content, leaving the tabs untouched

   <ul>          
    <li ng-class="{active:tab===1}"> 
      <a href ng-click="tab = tab==1 ? a : 1">Tab1</a>               
    </li>           
    <li ng-class="{active:tab===2}"> 
       <a href ng-click="tab = tab==2 ? a : 2">Tab2</a>              
    </li>           
    <li ng-class="{active:tab===3}"> 
      <a href ng-click="tab = tab==3 ? a : 3">Tab3</a>           
    </li>     
       </ul>      
    <br><br> 
    <p ng-show="tab === {{$index}}" ng-repeat="data in data">
       <a ng-repeat="value in data.value">{{value.name}}</a>
    </p>                   

But when I attempted to add ng-repeat to both tabs and content, it didn't work as expected

<ul>          
    <li ng-repeat="data in data" ng-class="{active:tab==={{$index}}}"> 
      <a href ng-click="tab = tab=={{$index}} ? a : {{$index}} ">Tab1</a>               
    </li>                      
     </ul>
    <br><br> 
    <p ng-show="tab === {{$index}}" ng-repeat="data in data">
       <a ng-repeat="value in data.value">{{value.name}}</a>
    </p>                   

I thought I followed logical steps while writing the code, but for some reason, it's not functioning properly. Can anyone provide some help please?

Answer №1

Avoid using interpolations {{}} within the expression, ng-class/ng-show requires an angular expression so consider:

   <li ng-repeat="data in data" ng-class="{active:tab===$index}"> 
      <a href ng-click="tab = (tab== $index ? a : $index)">Tab1</a>               
    </li>                      
     </ul>
    <br><br> 
    <p ng-show="tab === $index" ng-repeat="data in data">
       <a ng-repeat="value in data.value">{{value.name}}</a>
    </p>   

Keep in mind that $index starts from 0, not 1. Additionally, note that ng-repeat generates child scope so be mindful of the implications of child scope inheritance.

In your controller:

//initialize:-
$scope.tab = {selected :0}

and

$scope.setTab = function(index){
     $scope.tab.selected = index;
}

$scope.isSelected = function(index){
   return $scope.tab.selected === index;
}

and

    <li ng-repeat="data in data" ng-class="{active: isSelected($index)}"> 
      <a href ng-click="setTab($index)">Tab1</a>               
    </li>                      
     </ul>
    <br><br> 
    <p ng-show="isSelected($index)" ng-repeat="data in data">
       <a ng-repeat="value in data.value">{{value.name}}</a>
    </p>   

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

The performance of the Ionic app is significantly hindered by lagging issues both on Google

After starting to work with the ionic framework, I noticed a significant change in performance when testing an android app on Chrome for the first time. It was fast and responsive until I added a button that led to a screen with navigation bars, side men ...

Display the image before submitting it on Shiny platform

I'm currently developing a Shiny app that enables users to upload images directly to the server. I am wondering if there is a way to display the image on the screen without going through the process of uploading it first and then receiving the rendere ...

Why isn't my file being recognized post-multer middleware?

I am currently facing an issue with uploading a filename and a post (similar to 9GAG style) to my MySQL database, and storing the file in my project folder. The front end is able to retrieve the file without any problems (verified through console.log), but ...

How to upload a document to Alfresco using JavaScript API

I am facing an issue while attempting to upload a file from a node application to my local Alfresco server. I have been able to login, create, and delete folders successfully, but I am encountering difficulties when trying to upload files. let AlfrescoApi ...

Using Node and Express to handle form submissions, we can create a POST request that sends data to the server

Despite my ongoing learning journey, I am feeling a bit frustrated as I struggle to find a solution for this particular issue using ES6 Javascript. Currently, I have a straightforward todo-list web-app where everything is managed and displayed through the ...

angular.filter function includes groupBy, toArray, and fuzzy operations

I am currently implementing the angular.filter library in AngularJS found here: angular.filter and my objective is to utilize ng-repeat on the following data structure. Upon combining the three directives - groupBy, toArray: true, and fuzzy: search, I enc ...

What is the best way to transfer a variable to an isolated scope function?

I have set up a directive as shown below - <div data-my-param-control data-save-me="saveMe()"></div> Within the directive controller, I have connected the saveMe() function from the controller to an isolated scope like so - function MyParam ...

Tips for storing Ajax request information into separate variables

Seeking a way to store data returned from an ajax call into separate variables. Ajax Call $.ajax({ url: 'fetch_lat_lng.php', type: 'GET', dataType: "html", success: function(data) { //executed on successful response ...

Data Malfunction Leads to Failure of Ajax POST Request in Client-Side Operation

I am facing an issue where my Ajax POST calls are failing when I attempt to assign complex JavaScript objects to the [data] key/value pair after using JSON.stringify() for serialization. Could anyone advise on what additional ajax call configuration is re ...

Eliminating symbols such as $, @, and % from a string using jQuery

Need help with removing special characters like $, @, % from a given string. var str = 'The student have 100% of attendance in @school'; Looking for a way to strip % and $ symbols (or any other special characters) from the above string using jQ ...

Can anyone help me figure out the best way to test for observable errors in Angular2?

I'm currently working on creating unit test cases for my Angular2 components. Up until now, the test cases have been running smoothly. However, I've run into some issues with my asynchronous calls. For example, I have a method for creating a n ...

The user interface design transforms as a PDF file is being generated through html2pdf

I am experiencing an unusual problem while using html2pdf to convert an HTML page to a PDF file and download it. The conversion process is successful and the PDF file is downloaded without any issues. However, when I click on a button to generate the file, ...

What are the steps for generating and implementing shared feature files in Cucumber?

I am currently utilizing Cucumber to define some tests for the project I am working on, but as the application grows larger, I find myself in need of a more efficient structure. project | feature_files | | app1.js | | app2.js | | app3.js ...

width of the cells within the grid table

What is the best way to ensure the width alignment of cells in both the header and main section? I have highlighted the correct option in the image with green checkmarks. Check out the image here. Here is my example and solution: View the code on CodePen. ...

Adjust the size of the text and save it in a cookie for later use

I am in need of a script that can dynamically increase and decrease the font size on a website while retaining the user's chosen setting even after they return to the site. I believe utilizing cookies is the way to achieve this functionality. Despite ...

Multiple 'keydown' events are accumulating with Ajax activated

When the search field is focused, I am loading JSON into the browser and then iterating over the JSON objects to find real-time results 'on keydown'. The issue I'm encountering is detailed in the console after the initial block of code Aja ...

Possible issue with accurate indexing causing caption error with API images in React

Continuing from: Implementing a lightbox feature in react-multi-carousel for my ReactJS app My application utilizes react-images for the lightbox functionality and react-carousel-images for the carousel. The API provides a title and image data. The issue ...

Tips on using the $.grep() method in jQuery to filter dynamic inputs

When using the "grep" function in jQuery to filter data, the code sample below works well for static conditions. However, if the "name" and "school" parameters have multiple values, how can we filter them? Let's say I receive the name and school from ...

The AJAX request to the URL is failing

Creating a webpage with the ability to control an IP camera's movements such as moving up and down or zooming in and out involves calling specific URLs. While trying to implement this feature asynchronously, I encountered issues using AJAX which did n ...

Attempting to use jQuery AJAX to submit data without navigating away from the current webpage

Trying to implement the solution provided in this post where I am trying to send data to a PHP page and trigger an action, but unfortunately, it seems to just refresh the page without any visible outcome. Even after checking the network tab in the element ...