What could be causing the issue with ng-show in Angular?

When a user clicks on an icon, I want to display a drop-down menu. I attempted to use the ng-show directive for this purpose, but unfortunately, the drop-down menu is not appearing.

  • I created an icon ..., and when clicked, I attempted to open the drop-down menu using ng-show. However, it is not displaying as expected. Why could this be happening?

Below is the code snippet:

http://codepen.io/naveennsit/pen/pbPJEN

$scope.showDropDown=function(e){
    alert('show drop down');
    $scope.showDropDown=true;
    e.stopPropagation();
  }

HTML

<ul class="dropdown-menu" ng-show='showDropDown'>
                    <li><a href="#">Edit</a></li>
                    <li><a href="#">Delete</a></li>

                </ul>

Answer №1

In order to display the dropdown menu when clicking a button, you need to call the action to set the $scope.showDropDown in ng-click. This means using a function to set the value.

JS

 $scope.showDropDownFunction = function(e){
    alert('show drop down');
    $scope.showDropDown=true;
    // If you want a toggle effect (hide/show) on each click, use
    //$scope.showDropDown= !$scope.showDropDown; 
    //instead
    e.stopPropagation();
  }

HTML

 <button ng-click='showDropDownFunction()'> show menu </button>
 <ul class="dropdown-menu" ng-show='showDropDown'>
   <li><a href="#">Edit</a></li>
   <li><a href="#">Delete</a></li>
 </ul>

This setup allows the ul element to be shown when the button is clicked.

For more information, refer to the documentation: https://docs.angularjs.org/api/ng/directive/ngShow PS: Check out the last example provided there.

Answer №2

avoid the need for defining a function

<button ng-click="displayMenu=true" >Display Menu</button>
<ul class="menu-dropdown" ng-show="displayMenu">
    <li><a href="#">Update</a></li>
    <li><a href="#">Remove</a></li>
</ul>

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

What is the best way to utilize $emit in order to postpone the execution of a function for the following minute?

Within my service code, I have the following snippet: $rootScope.$emit('rootScope:success-response') Additionally, there is a function that runs every 60 seconds in another part of the code: this.$interval(function () { myFunc() ...

I encountered an issue with my node/express server where Res.json is causing a

I am encountering an issue with a 100mb object that I am trying to return using a GET request: function completeTask(req, res) { // generates a large object on a child process, then sends it back to the main process res.json(bigObject); // <--- p ...

Protractor - Error: prop is undefined

Need assistance in identifying an error. Currently, I am learning Protractor and attempting to create a basic test using Page Object. //login_pageObject.js let loginContainer = function() { this.usernameInput = $("input.login-form-01"); this.passwordInp ...

Utilize the controller data to display information on a day-by-day basis

In my attempt to design a weekly calendar using AngularJS, I am facing challenges when it comes to inserting events into the 7 boxes representing each day of the week on the calendar. The current HTML structure is as follows: <div class="week"> ...

What is the best way to experiment with angular-ui-tinymce within a tabset using Plunker?

I am currently experimenting with angular-ui-tinymce by incorporating it into a Plunkr. Can anyone assist me in integrating the necessary files for angular-ui-tinymce into the Plunkr so that I can showcase its functionality? Currently, I have this Plunker ...

Having difficulty generating the HTML list of maps using the code provided in AngularJS

Could someone assist me in displaying the value in HTML for the following code written in angular js? Here is the angularjs controller code : $scope.arr=[new Map().set("MYKEY","MYVALUE")]; And here is the corresponding html code : {{arr}} Any tips on ...

I am looking to implement a dropdown menu that appears after clicking on an image. Any suggestions on how to achieve this using

I've been experimenting with adding a dropdown class, but I'm feeling a bit lost on where to start. Here's a snippet of code that shows what I'd like to do to add a dropdown menu: <span id="dropdown-info" ng-init= "myVar='i ...

Unusual actions observed in ajax rails form calculator with pre-filled values

I have a form calculator in Rails that utilizes AJAX without a database. While it functions properly, I have noticed some peculiar behavior which I would like to address. Firstly, I navigate to the /interest_calculator page Then, I input 10 into @first_0 ...

PHP/MySQL clarification regarding time slots

Could someone assist me with this discussion I found? Due to my low reputation, I am unable to comment for further clarification. Although I grasp the solution provided in the mentioned discussion, I am struggling to comprehend how to pass the data to my ...

The improper utilization or replacement of Jest mock in an Angular standalone component's unit test is causing issues

Presented here is a streamlined Ionic/Angular component with unnecessary code removed. import { IonicModule, ModalController } from '@ionic/angular'; @Component({ selector: 'my-component', templateUrl: 'my-component.html' ...

Is there a way to detect when a CSS background image has finished loading? Does an event trigger upon completion?

I am facing an issue with my sidebar widget where there is an image background in place. On top of this, I have a search input form but I don't want the input to display until the image has fully loaded. Is there a way to add a load event handler to ...

Find out if OpenAI's chat completion feature will trigger a function call or generate a message

In my NestJS application, I have integrated a chat feature that utilizes the openai createChatCompletion API to produce responses based on user input and send them back to the client in real-time. Now, with the introduction of function calls in the openai ...

Select elements using jQuery in events while excluding others

I need to prevent form submission when the user presses Enter, except for three specific inputs. To prevent form submission on Enter key press, I can use this code: $(document).keydown(function(event){ if(event.keyCode == 13) { event.preventDefault(); re ...

Make sure the "Treat labels as text" option is set to true when creating a chart in a Google spreadsheet using a script

I am currently working on a script using Google Spreadsheet Apps Script interface and I need to set the marker for 'Treat labels as text' to true. Despite searching through App Script documentation, I couldn't find any specific mention of t ...

Determine whether the function name in a given string matches a JavaScript or PHP

I am currently attempting to highlight code and eventually sanitize the HTML, but I am encountering an issue with my regex not matching just the function name and parameters. Regex is not my strong suit and can be quite confusing for me. Additionally, when ...

JSON and autocomplete feature causing performance problems

Developing an application that functions both online and offline using technologies like application cache and local storage is my current project. Utilizing jQuery mobile along with a jqm autocomplete solution from , I aim to create a seamless user experi ...

NPM's twitter-v2 is throwing a 'TypeError: url_1.URL is not a constructor' error

Currently, my focus is on developing a Twitter bot. I am in the process of posting a Tweet using my account configured to work with Twitter API v2. This involves utilizing NPM's twitter-v2, along with Webpack 5.74.0 and the latest version of NodeJS. ...

Utilize a React Switch Toggle feature to mark items as completed or not on your to-do list

Currently, I am utilizing the Switch Material UI Component to filter tasks in my list between completed and not completed statuses. You can view the demonstration on codesandbox The issue I'm facing is that once I toggle a task as completed, I am un ...

Tips for triggering jquery code when a variable containing a CSS attribute is modified

I have a specific requirement where I need to reset the scrollleft value to 0 on my wrapper whenever a particular CSS property changes. Although I am new to using jQuery and haven't worked with variables much, I believe I need to create a variable to ...

Sending a text parameter to the JavaScript Executor within a Selenium framework

Although this question has been asked before, I have searched for multiple answers and have not found a solution to my specific problem. if (driver instanceof JavascriptExecutor) { System.out.println("In try"); ((JavascriptExecutor)driver) ...