Having trouble with Elem bind not functioning properly with transclude set to true?

I've been attempting to transclude the content of an element and adjust its display property by binding a click event to one of its children. Unfortunately, it doesn't appear to be functioning as expected.

app.directive('apple', function(){

  function link(scope, elem, attr, ctrl, transclude) {
    elem.find('a.link').bind('click', function() {
      $('ul.menu').toggleClass('active');
    });

    transclude(scope, function(clone) {
      elem.html(clone);
    });
  }

  return {
    restrict: 'E',
    transclude: true,
    link: link
  };

});

HTML:

<apple>
  <a class="link" href="#">Show</a>
  <ul class="menu">
    <li>linky</li>
  </ul>
</apple>

Any thoughts on why this may not be working? Link to fiddle: http://jsfiddle.net/pb2q4zj4/1/

Answer №1

If you're looking to create a drop-down menu and want a simpler approach instead of diving into how transclude works, here's an alternative method;

Steps

<div ng-app ng-init="show=false">
    <div>
      <a ng-click="show = !show">Show</a>
      <ul ng-show="show">
        <li>linky</li>
      </ul>
    </div>
</div>

Additional JavaScript

angular.module('app', []);

Check out this demo on JsFiddle

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

Is there a way for me to access a user control property directly from the client side?

I'm in the process of developing several user controls that will be derived from my base class, BaseControl, which is a subclass of UserControl. The BaseControl class contains important features that I will need to utilize, including a string property ...

The issue of duplicate tabs arising in AngularJS and AngularUI Bootstrap when a new tab is added

As I strive to create a reusable directive, complete with an isolated scope, for every new AngularUI tab, I encounter a peculiar issue. Whenever I attempt to gather all the data within a tab and add a new one, Angular seems to duplicate my existing tabs an ...

Iterating through two variables and running tests every 1000 milliseconds

I am working with two variables: const frame = $('#avacweb_chat iframe'); let time = $('.date-and-time' , frame.contents()).last().text(); let newTime = setInterval(function() { let newT = $('.date-and-time' , frame.content ...

Nested object type checking is not being performed

I have been developing an Angular application and working with two interfaces: IAbc and IXyz. interface IAbc { id: number, name: string } interface IXyz { id: number, value: string | number | Date | IAbc[]; } In my code, I initialize a va ...

Having Trouble with window.location in Chrome Browser

In my code, there is a JavaScript function that utilizes window.location. Surprisingly, it runs smoothly in Firefox and Internet Explorer, but encounters issues while running on Chrome. I have tried testing it on both Ubuntu Hardy and Windows Vista opera ...

Determine the maximum value in the array using the Math.max method with

How exactly does Math.max.apply(null, arr); work? Let's take for example var arr = [45,25,4,65] ; Will it compare 'null' and '45' and return the maximum number between the two, like 45? After comparing, will it then compare t ...

Image transformation not rotating the image

I'm having trouble making an image of an X rotate 180 degrees when it's hovered over. Instead of rotating, the image just moves up and to the right. What am I missing that's preventing this from looking like a smooth 180-degree spin? .bl ...

Issue with default behavior of infinite scroll in Angular 4

I'm currently working on incorporating infinite scroll into my Angular 4 application. I've carefully followed all the guidelines provided on https://www.npmjs.com/package/ngx-infinite-scroll According to the documentation: By default, the dir ...

Pressing the "Enter" key will trigger a JavaScript function while the textbox is in focus

I am looking to enhance my search functionality on my website. <div class="TextField Large"> <input type="text" name="q" id="SearchBox" /> </div> <div style="height: 40px; text-align: right; position: absolute; top: 0px; right: 0p ...

The carousel feature in Angular JS is malfunctioning

My angular carousel is not working properly and I am facing issues. You can view the carousel here. The controller in my Angular code does not seem to be calling out the images. To see the source code, you can click here. I have followed the coding demon ...

Streamline your CSS selectors that share a common parent for more efficiency

.content p, .content ul, .content h1 { text-indent: 35px; } Is there a more concise way to write this selector, such as .content p, ul, h1 {}?                                                      ...

I am experiencing issues with the HTTPClient call not returning accurate JSON data within my Appcelerator Titanium application

Every time I attempt to retrieve information from a JSON file, I encounter an error. function search(e){ var url = 'https://www.dotscancun.com/createjson.php?id=100001'; var xhr = Ti.Network.HTTPClient({ onerror: function(e){ ...

Using Knex.js for Dataloader

The code worked perfectly fine until I made the update to dataloader: 2.0.0 const productLoader = new DataLoader(async keys => { const products: Product[] = await knex('product') .whereIn('id', keys) .select() const prod ...

Processing .obj file and converting it into a string format with the help of three

I have a unique challenge where my program creates .obj files at runtime and stores them as strings. I am now looking to load these generated .obj files using the load() function in three.js (OBJLoader). Currently, the load() function uses an HTTP request ...

Node.js directory hash

As I work on my project, I am looking to calculate the hash values of folders instead of just individual files. Imagine having 10 folders, each containing multiple subfiles. While I am familiar with various methods for getting the hash of files, I am curio ...

Implement a vertical bar to appear next to the tree view when it is expanded

I am struggling to implement a vertical line for the first level of list items when they are expanded and remove it when they are closed. I have tried using li::after but it doesn't seem to work. Can someone provide guidance on how to achieve this? T ...

Iterating over an array of objects and executing asynchronous operations

I am currently working with NextJS and managing an array of assets (images) within my state. The task at hand is to post these images to the API. To accomplish this, I have a specific object that handles the posting process using the following syntax: let ...

Express string declaration in a single TypeScript line

const restrictString = (str: string): string => str.match(/[ab]/g)?.join('') || '' Is there a way to restrict a string to only contain the characters 'a' and 'b' in a one-liner function? I am aware that this can ...

Ways to Resolve Issues with WordPress Update and Publish Failures

It has been quite some time since Gutenberg was introduced to us, and it seems like we all have a complicated relationship with it. Navigating the Block editor to create a post or page can be challenging, especially when it used to be so simple. But what& ...

Is it possible to utilize a Next.js image for triggering a form submission via onClick for Google OAuth?

Disclaimer: Novice User I have successfully created a custom signin page with email and social media authentication using Next.js. Now I would like to customize the styling by incorporating Next.js/Image to replace the submit button with a clickable icon ...