combine a pair of elements simultaneously

I recently developed a nested directive for a multitabbed form, and here is a simplified version:

<outer>
  <inner heading="a" src="'a.html'" active="true"></inner>
  <inner heading="b" src="'b.html'"></inner>
</outer>

This directive creates a tabset with ul/li elements. When a tab is clicked, the corresponding div becomes visible because its respective li is set to be active.

Below are my directives:

(function(){
  'use strict'

  angular 
    .module("testDirective", [])
    .directive("outer", outer)
    .directive("inner", inner);

  function outer(){
    return {
      templateUrl: 'outer.html', 
      transclude: true,
      controller: function($scope){
        var tabs = $scope.tabs = [];  
        this.addTab = function(_active, _name) {
          tabs.push({
            active : _active,  
            name : _name
          }); 
        }  

        $scope.toggle = function(ix){
          for (var i = 0; i <= tabs.length - 1; i++) {
                    tabs[i].active = false; 
                } 

                tabs[ix].active = true;
        }  
      } 
    }
  } 

  function inner(){
    return {
      require: '^outer',
      scope: {
        src : '=',
        active : '=',
      },
      templateUrl: 'inner.html',
      link : function(s, e, a, ctrl) {  
        ctrl.addTab(a.active, a.heading); 
      }
    }
  } 
})();

While I have successfully implemented most of the functionality, there is one aspect that is causing me some trouble: how can I efficiently show/hide content? You can see the full code on this Plunker.

Answer №1

Check out this solution:

Update made to index.html:

<wrapper>
  <content title="x" source="'x.html'" location="0" isactive=true></content>
  <content title="y" source="'y.html'" location="1"></content>
</wrapper>

Content of the elements:

<ng-include ng-class="checkActive()" source="source"></ng-include>

A new function was added to the main function:

this.checkActive = function (location) {
      if(tabs[location] && tabs[location].isactive){
        return "show";
      } else {
        return "hide";
      }
};

Adjusted link inside content element like this:

link : function(scope, element, attr, controller) {  
    controller.addTab(attr.isactive, attr.title);
    scope.checkActive = function () {
      return controller.checkActive(attr.location);
    };
}

http://example.com/sample-link

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

I'm seeking assistance in enhancing this code within tb for extracting values using JavaScript

Currently, I am extracting values from a table using JavaScript. The code seems to be functioning correctly, but I am looking for ways to optimize it and ensure that it is compatible with most modern browsers. The list in the table undergoes frequent chang ...

Sending Parsed Information to Callback for Flexible Use

Is there a way to pass the value of coins, or even better, currency to my callback function so I can freely use the parsed JSON data in other functions? function fetchJSON(path, callback) { var jsonReq = new XMLHttpRequest(); jsonReq.onreadystatechang ...

Angular and Three JS collaboration leads to misplacement of canvas rendering

Currently in the process of developing code for a website, I am attempting to integrate Three JS with Angular. After conducting some research, I have found that this combination is feasible and I have successfully merged these two libraries. However, I am ...

Problem with Closing Alerts in Angular UI Bootstrap

Utilizing Angular UI Bootstrap to create Alert boxes, the code used is as follows: <alert data-ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert> The output is as expected: Inquiring: Is there ...

How to Transfer Information Between Two React Components

I recently started learning React and I'm not sure how to approach this task. Basically, I have a list of cars and when the user clicks on each car, it should display detailed information about that specific car in a full-page slide. This is my curr ...

Chrome causing window.open to fail

I am currently working on developing a Cordova PhoneGap app and facing an issue with popping up a window for Facebook login after clicking a button. The popup works fine on iOS devices, but when I try debugging with Chrome, nothing happens even though th ...

Once this code is executed, Javascript ceases to function

I have developed a code snippet to create a typing effect similar to a command console. While the code is functioning well on its own, any additional code I add after it seems to malfunction. I've spent quite some time troubleshooting this issue witho ...

The Bootstrap navigation menu fails to extend the parent div when toggled

When I toggle the button to show the menu on small screens, the menu overflows the parent div with id=contents instead of pushing it down. How can this issue be fixed? Here is the code: <body> <nav id="header" class="navbar navbar-default"& ...

The MUI5 drawer overlapping a modal dialog

Is it possible to use MUI5 to open a side drawer on top of a modal dialog that triggers it? Currently, the dialog triggers the side drawer but it opens behind this. It appears as though a modal dialog (drawer) is attempting to open over an already-opened ...

The Angular UI Datepicker is not reflecting changes in scope variables within the dates-disabled function

I'm currently working with AngularJS and the Angular UI Bootstrap. I've encountered an issue that I initially thought was related to scope, then initialization, but now I'm stuck trying to figure out what's causing the problem. I' ...

What is the best way to hide the div when scrolling to the top?

Is there a way to hide the circle once it's scrolled to the top? Currently, I can scroll the circle to the top but it remains visible. Upon further exploration, I found that clicking on the circle scrolls it to the top and then on a second click, it f ...

Storing notes using HTML5 local storage

I recently developed a unique note-taking web application where users can create multiple notes and switch between them using tabs on the left side. My next step was to implement local storage for saving these notes, so I inserted the following code: $(d ...

Error: The package name "@angular/compiler" is not valid in Npm

After successfully installing Ionic2 for a new project, I proceeded to set up a continuous integration build. However, running npm install on the build server resulted in failure with the following error: npm ERR! Windows_NT 6.2.9200 npm ERR! argv "C:&bso ...

When the page is scrolled to 50 pixels, a modal pop-up will appear

I attempted to use cookies to store a value when the user clicks on a popup to close it, ensuring that the popup does not show again once closed. However, I am encountering an issue where the popup continues to open whenever I scroll, even after closing it ...

Can you explain the concept of the "node module wrapper function" in the context of Node.js?

Can someone explain to me the concept of a "module wrapper function" and how it affects my code? (function (exports, require, module, __filename, __dirname) { }); ...

How to retrieve the type of a computed keyof T as a generic type within TypeScript

I am working with two different interfaces: interface PersonRequirements{ user:string, password:string, id:number } export interface Requirement<R> { name: keyof R & string, save: () => any,/* I want this return type to be ...

A JavaScript pop-up box with two windows

Struggling to create two separate dialog boxes using this code. The issue is that when I add the code for the second dialog box, it only appears if the first one does too. Here's the code for the first dialog: function showPopUp(el) { var ...

Function for masking phone numbers is adding additional "x's" to the input field

My phone number formatting script is supposed to add dashes after the third and sixth characters, as well as insert an "x" after the 10th character for extensions. However, it is adding extra "x's" after the 12th character, resulting in the incorrect ...

When a <a href> link is clicked, the Struts action should open as a popup

I have a form on my jsp page, <form action="test1_action" name="test" method="post" id="test"> Additionally, I have two distinct links: link1 and link2. When link1 is clicked, the form should be submitted with the action test1_action. $('#l ...

The d3.js Force Directed Graph is not working as expected

I keep encountering an error in the dev console whenever I attempt to run my code. The errors are as follows: Error: missing: 1 3d-force-graph:5:22166 TypeError: r.attributes.position is undefine You can view the live version of the faulty code here: He ...