How can I unbind the scope from two select list buttons that are triggering each other on click in angular.js? Need help deselecting buttons connected to the same scope

Check out this JS Fiddle

When you click on one button, it triggers the other. In my application, clicking on categories changes the URL to /:catId, which populates the list of animals where animal.catId = catId. I've tried organizing the arrays within functions or objects but they still seem to trigger each other. It appears that both buttons are pointing to the same object in the scope or model. How can I separate them? Is it reasonable to process both lists in the same factory?

This is the HTML:

<div ng-controller="MyCtrl">
   <div class="btn-group">
       <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
           Categories<span class="caret"></span>
       </button>
       <ul class="dropdown-menu" role="menu">
           <li ng-repeat="category in animals.categories"><a>{{category}}</a></li>
       </ul>
       <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
           Animals <span class="caret"></span>
       </button>
       <ul class="dropdown-menu" role="menu">
           <li ng-repeat="category in animals.all"><a>{{animal}}</a></li>
       </ul>
    </div>
</div>

And here is the Javascript:

var myApp = angular.module('myApp', []);

myApp.factory('animals', function(){
       var data = 
        [
            {category: "mammal", name: "dog", categoryID: "MAM"},
    {category: "fish", name: "pickerel", categoryID: "FIS"},
    {category: "mammal", name: "cat", categoryID: "MAM"},
    {category: "mammal", name: "monkey", categoryID: "MAM"},
    {category: "bird", name: "budgie", categoryID: "BIRD"}
        ];

   var collect_categories = function(o, e) {
    o[e.categoryID] = e.category;
    return o;
};
var unpack = function(name, id) {
    return { name: name, catID: id };
};


    var all = [];
    var categories = [];

    var init = function() {

         _(data).each(function(item) {
             all.push({
             title: item.name,
             })
         });

          _(data).chain()
             .reduce(collect_categories, {})
             .map(unpack)
          .each(function(cat) { categories.push(cat)})
             .value();

    }();

    return {
        all: all,
        categories: categories
    }
});


function MyCtrl($scope, animals){
    console.log("animals", animals);
    $scope.animals = animals;

}


MyCtrl.$inject = ['$scope', 'animals'];

Answer №1

http://jsfiddle.net/mikeeconroy/9hQHN/24/

UPDATE: Check out this revised fiddle with the dropdowns filled: http://jsfiddle.net/mikeeconroy/9hQHN/25/

Cliff, it appears that having both buttons in the same bootstrap button group is causing an issue where both drop downs are being opened simultaneously. To resolve this, you should separate them into distinct button groups.

<div class="btn-group">
   <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
       Categories<span class="caret"></span>
   </button>
   <ul class="dropdown-menu" role="menu">
       <li ng-repeat="category in animals.categories"><a>{{category}}</a></li>
   </ul>
</div>
<div class="btn-group">
   <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
       Animals <span class="caret"></span>
   </button>
   <ul class="dropdown-menu" role="menu">
       <li ng-repeat="category in animals.all"><a>{{animal}}</a></li>
   </ul>
</div>

It seems like there might be an issue with the service not providing the correct data to the controller for the animals section, which could affect the categories shown in the drop down menus.

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 am facing an issue with the responsiveness of the mat-card component within a div in

Is it possible to display several small paper cards in a div so that they wrap around the container? ...

Unable to change data in table TD using AJAX and PHP received JSON array

I am currently facing an issue with a PHP-AJAX script that is responsible for deleting financial rows from a table. The PHP script is functioning correctly and successfully deleting the rows. However, the problem arises within the success function of the A ...

What might be causing my dropdown menu to be unresponsive in HTML?

In this HTML code, I opted to use images as dropdown elements instead of traditional buttons or text. /* Dropdown Button / .dropbtn { background-image: url(Images/Buttons/Resources3.png); width: 110px; height: 40px; } / The container <div> - neede ...

Minimalistic command-line calculator in JavaScript (utilizing only native functions for concise code)

My latest project involves creating a calculator in various programming languages, and I've hit a roadblock with Javascript. Below is my simple calculator in Python3 that efficiently operates in just 2 lines using the eval() function: #!/usr/bin/env ...

Control the activation/deactivation of a dropdown menu depending on the selection of another dropdown menu

I need assistance with creating functionality for two dropdown lists. <select id="cat_user"> <option value="Super Admin">Super Admin</option> <option value="Event Admin">Event ...

Chaos in asynchronous Node.js execution orders

Here is a snippet from my code: async.forEachOfSeries(dates, function(mydate, m, eachDone) { eachDateParse(Name,Place,Strategy, eachDone) }, function(err) { if (err) throw err; console.log("All Done!"); callback(); } ); While async.f ...

When launching a modal window, the ability to dynamically change the URL and seamlessly transition into the modal window using Bootstrap 3, AngularJS, and CSS

Hello, I am looking to create a modal window that changes the URL when opened. For example, if a user clicks on a link with #123, the URL should change to include #123 after the slash. Additionally, I would like to be able to send someone a link directly ...

In the realm of yup Schema validation, it is deemed necessary that either all fields be required or none at all, should any single field contain non

Scenario: I have 4 input fields that need to be validated using Yup validation schema. If the user enters a value in any of the fields, then the other 3 fields must also be filled out. If the user leaves all 4 fields empty, then none of the fields ar ...

Using the getValue() method to retrieve the date

Snippet of HTML Code: <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text">From</span> </div> @(Html.DevExpress().BootstrapDateEdit("dtReturnDateFrom").EditFormat(Edit ...

Is a token system necessary for authentication in AngularJS and Devise Rails?

I am currently working on a project that combines Rails and AngularJS. I am interested in setting up an authentication system using the Devise gem, but I'm not quite sure how to go about it. I have come across mentions of using the :token_authenticata ...

Guide to incorporating a simple animation into a component using Vue.js

As I explore the official Vue documentation on animation, I find myself puzzled about how to integrate the example provided on the Vue website: Vue.transition('fade', { css: false, enter: function (el, done) { // element is already inser ...

Manage the orientation of an object circling a sphere using quaternions

My latest game features an airplane being controlled by the user from a top-down perspective, flying over a spherical earth object. The airplane has the ability to rotate left or right by using the arrow keys on the keyboard, and can accelerate by pressing ...

Displaying summaries for all items (including those that are not currently visible) in the Kendo UI Grid while paging or grouping

Is it possible to display aggregates for all items in a grid data source while using paging and grouping? In my list of 1000+ items with columns like Description, Type, Cost, I am fetching only 200 items at a time using OData. However, when viewing the ag ...

Unable to render canvas element

Hey guys, I'm trying to display a red ball on the frame using this function but it's not working. I watched a tutorial and followed the steps exactly, but I can't seem to figure out what's wrong. Can someone please help me with this? I ...

Executing unique calculations on Kendo UI Grid Columns

For experienced users, this may seem simple, but as a newcomer, I'm struggling with a basic arithmetic task. I want to multiply one of the column values (DealValue) by 0.05 in my Kendo grid setup. Despite looking through the Kendo docs, I couldn' ...

Revolutionary Approach to Efficiently Handle Multiple rows with Jquery

Greetings to everyone, I am currently in the process of developing an application that retrieves data from a database via AJAX by calling a .php file. Within this app, I have a table with 4 columns. The first two columns consist of dropdown menus, the thi ...

Displaying Laravel's validation errors within the Ajax success event

When attempting an Ajax request and anticipating a failure, I find that the error response is unexpectedly being received within the success event instead of the fail event. I require Laravel's response to be directed to the Ajax fail event, as I int ...

Progress Indicator on my online platform

I've been attempting to remove a loading bar from my website, but I can't seem to locate it in the site files. I even tried using Google Chrome's inspection tool, but I couldn't pinpoint the loader. Can someone please assist me? Visit ...

Saving the initial state value in a variable in Vue.js is a crucial step in managing and

My dilemma involves an object in the Vuex store containing data that I need to utilize within a component. I have successfully accessed the data using a getter in the component. However, I am facing a challenge in preserving the initial value of this objec ...

Generating an image from HTML code using PNG format

A web application that I developed allows users to dynamically create images using JavaScript. The application utilizes jQuery for users to place, resize, and drag div elements within a <div> container. Once the user is happy with their layout, they ...