Using identical filters for two ng-repeats causes issues in AngularJS, as it only seems to work in one of them

I am encountering an issue with my search box where I am attempting to filter two items, but only one is filtering correctly.

Here is the code snippet in question:

<!--this is the input-->
<input type="search" placeholder="Sports finder" ng-model="query">
<!--this is the working filter-->
<div ng-repeat="sport in sports | filter:query"
     ng-show="sport.leagues.length">
<!--this is the problematic filter-->
<div ng-repeat="league in sport.leagues">

I have attempted to fix it by using the following:

<div ng-repeat="league in sport.leagues | filter:query">

According to some feedback I received, the filter is not functioning properly because the last expression is returning an object instead of an array.

Below is the section of code where I am extracting the data:

AuthFactory.getCustomer().then(function(customer) {
  $scope.customer = customer;
  SportsFactory.getSportsWithLeagues(customer).then(function(sports) {
    $ionicLoading.hide();
    if (sports.length) {
      $scope.sports = sports;
      console.log($scope.sports);
      console.log('on controller js I am an ' + typeof($scope.sports));
    }else {
      AuthFactory.logout();
    }

The first console log returns:

[Object, Object, Object, Object, Object]

And the second one outputs:

on controller js I am an object

Let's say we open up that data and see that it returns this structure:

>0: Object
>1: Object
>2: Object
>3: Object
>4: Object
>5: Object

If we dive into >0: Object, it reveals:

$$hashKey: "object:54"
checked: true
id: 26
> leagues: Array[3]
name: 'Live Betting'
priority: 0
> proto: Object

Essentially, the filter functions as expected on the initial

[Object, Object, Object, Object, Object]
but does not work on the nested leagues: Array[3].

If this explanation is unclear, please refer to these screenshots:

and:

This situation is quite perplexing. Here is a Plunkr example that works as intended, featuring JSON data taken from the network console.

For instance, searching for International or Greece yields the correct filters:

International Basketball
GREECE A1

In contrast, when typing just Greece in my app, the filter fails to return any results.

UPDATE

Please review this Plunkr, which is not functional but provides access to my complete code: http://plnkr.co/edit/bfU8ynzmsQZk57dYXFUj?p=preview

Answer №1

Don't be deceived by console.log. The properties shown when you click on an object in the console may not reflect the true state of the object at the time of printing.

For a more accurate view, use this code:

console.log(angular.toJson($scope.sports, true));

This will show you the precise state of your object in that moment.

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

Clicking on the Edit button does not result in any changes to the HTML on the page following a table

My current setup involves a button that is supposed to toggle between 'Add New User' and 'Update User' elements when clicked. However, it seems to work only when the table has not been filtered. Once I apply any kind of filtering on the ...

Acquiring ownership of or distributing directive control

I need to implement directives A and B, which have a lot of similarities. I am looking for a way for directive B to inherit the controller from directive A. In simpler terms, the controller function specified in directive A's definition object should ...

The outcome of the Javascript Calculator is showing as "Undefined"

I've been attempting to create a calculator using JavaScript, but I'm facing an issue where the 'operate' function keeps returning Undefined, and I can't seem to figure out why. I suspect that the problem lies with the switch state ...

Utilizing Knockout to Load JSON Data in DevExtreme

As a newcomer to both javascript and devextreme, I am currently navigating my way through. I successfully created a webpage that loads a json file, but I'm facing challenges implementing it using devextreme. I have a dxDataGrid where I intend to disp ...

The challenges of using Three.JS and Blazor: Solving Black Canvas and Console Errors in WebGL

Exploring the world of Blazor web assembly, I embarked on a project to harness the power of JSInterop with Three.JS to draw lines. Following the guidelines provided in their tutorials available Here, I diligently installed Three.JS using npm and webpack, w ...

How come WhatsApp is not recognizing my og:tags correctly?

I'm having an issue with my webpage where the og:tags generated for link previews on Facebook work fine, but they can't seem to be scraped by WhatsApp. Even though the tags are correct in the source code of the site, WhatsApp shows an error messa ...

Bring in a JavaScript function into NodeJS without the need for importing any extra dependencies

Is there a way to import a zero-dependencies function zdfun without loading all the other dependencies in the module/file foo.js where it resides? While the ideal solution would be to extract zdfun into its own module and import it into foo.js, this may n ...

What causes a browser to redirect when trying to update the value of the alt field in a Wordpress media image?

Attempting to create a bookmarklet for the Wordpress image gallery manager that triggers the sidebar when an image is clicked. The sidebar contains fields for alt text (input), legend, and description (both textarea). <a href="javascript:var t=document ...

"Move a div towards the mouse's location by animating it with JavaScript when

I've been working on making the ball element move and shrink towards the goals upon mouse click. I successfully created a function that captures the mouse click coordinates in the goals, but I'm facing challenges with the ball animation. I consi ...

Combining ng-bind-html with highlight.js

I am currently working on implementing syntax highlighting for all code elements within dynamically added HTML content that will be appended to the existing page using ng-bind-html. <div ng-repeat="activity in activities"> <h3>{{activity.t ...

Combining strings with JQuery

Looking for help with a code snippet <script> function goToDetails($i){ $(function(){ var transValue = $('#trans'+$i).html(); var mileageValue = $('#mileage'+$i).html(); var engineValue = $('#eng'+$i).html ...

How to set up a multi-select box for tagging purposes

I tried to implement a multi select box to create tags using the code below. I downloaded select2.min.css and select2.min.js from https://github.com/select2/select2, then copied them into the project's css and js folders. Here is the HTML code snippe ...

The component encountered an error because the element type provided was not valid

I've encountered an error that has me stumped: https://i.sstatic.net/glSvr.png I've been trying to locate the HeaderSegment, but it seems to be missing from my project. I've checked for any imports, such as: import HeaderSegment from &apos ...

Transmit the JavaScript array via AJAX to PHP server

Is it possible to transfer a JS array created using the .push function to another PHP file? I have included the code snippets for the two files involved, game.js and gallery.php. In Game.js: imgarray.push({"img_name": img_name,"x": x_value,"y": y_value,"w ...

Dynamic item addition feature activated by button on contact form

I am looking to create a form that includes the standard name, phone, email fields as well as a dropdown for selecting products and a text box for inputting quantity. The unique aspect is allowing users to add multiple products (dropdown and quantity textb ...

Update the image with a new one using jQuery and PHP, then refresh the page to see

I'm currently utilizing simpleImage, a PHP image manipulation library. My aim is to externally rotate an image using AJAX and replaceWith functions. It successfully replaces the image, but unfortunately it doesn't refresh after rotation. Here&ap ...

The AngularJS directive scope does not appear to be properly binding to the value

I'm new to Highcharts.js with Angular.js and I'm having trouble with the directive scope not binding to the value. Below is my code: HTML - it's working inside these expressions {{xaxis}} {{seriesdata}} <div class="container" ng-control ...

Is it a wise choice to implement frames in AngularJS?

I am in the process of developing an AngularJS application with a three-panel main view, resembling a mail system with a navigation bar on the left. The right pane is divided into two sections; selecting an item from the navigation bar populates the top-ri ...

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() ...

Having trouble loading my script in the frontend plugin?

After much testing, I have discovered that when I place this code before the get_header() function, the script is successfully loaded. However, when I move it after the get_header() function, it no longer works as intended. It's important to note that ...