ng filtering with a controller-defined scope

I am currently working on a webpage with AngularJS and I am looking to implement some filters on the site.

Here is the HTML code I have:

<div ng-repeat="data in datas | filter:{area:course} | filter:{subject:subFilter} | filter:{city:cityFilter}">
                        <h5><span class="text-warning">#</span>&nbsp;{{data.intrest}}</h5>
                        <div class="row">
                            <div class="col-xs-3">
                                <h5 class="text-info">Name</h5>
                                <p>{{data.name}}</p>
                            </div>
                            <div class="col-xs-3">
                                <h5 class="text-info">Subject</h5>
                                <p>{{data.subject}}</p>
                            </div>
                            <div class="col-xs-3">
                              <h5 class="text-info">Address</h5>
                                <p>{{data.city}}, {{data.state}}</p>
                            </div>
                            <div class="col-xs-3">
                                <a href="#/view"><button class="btn btn-warning" style="margin-top:10px;">View</button></a>
                            </div>
                        </div>
                        <hr>
                    </div> 

This is my controller file:

app.controller('mainController',['$scope','$http','$routeParams',function($scope,$http,$routeParams){
$http.get('assets/newtab.json').success(function(data){
    $scope.department = data;
    $scope.whichItem = $routeParams.itemId;
    $scope.course = $scope.department[$scope.whichItem].course;
});
$http.get('assets/engineering.json').success(function(data){
    $scope.datas = data;
    $scope.whichItem = $routeParams.itemId;
    $scope.optItem = [
        {title:"Random",value:" "},
        {title:"Question Paper Setting",value:"Question Paper Setting"},
        {title:"Question Paper Passing Board",value:"Question Paper Passing Board"},
        {title:"Question Paper Post Auditing",value:"Question Paper Post Auditing"},
        {title:"Question Paper Evaluation",value:"Question Paper Evaluation"},
        {title:"Member for board of study",value:"Member for board of study"},
        {title:"Member for academic council",value:"Member for academic council"},
        {title:"Result passing board",value:"Result passing board"},
        {title:"Invigilation",value:"Invigilation"},
        {title:"Door Valuation",value:"Door Valuation"},
        {title:"Exam squad",value:"Exam squad"}
    ];
});
}]);

Snippet from engineering.json and newtab.json:

engineering.json

[{"id":"1","name":"Sam","dob":"","age":"21","gender":"","department":"16\/03\/1995","area":"Nuclear Medicine Technology Course","institution":"Park College of Technology ","city":"Tiruppur","state":"Tamil Nadu","intrest":"Question paper evaluation","mobile":"","email":"[email protected]","password":"jaya","subject":"Computer Networks"},{"id":"2","name":"jaya","dob":"","age":"21","gender":"","department":"16\/03\/1995","area":"Nuclear Medicine Technology Course","institution":"Park College of Technology ","city":"","state":"","intrest":"","mobile":"","email":"[email protected]","password":"jaya","subject":""},{"id":"3","name":"jaya","dob":"","age":"21","gender":"","department":"16\/03\/1995","area":"Nuclear Medicine Technology Course","institution":"Park College of Technology ","city":"","state":"","intrest":"","mobile":"","email":"[email protected]","password":"jjjj","subject":""}]

newtab.json

[{"Department":"Arts","course":"B.A. English"}, {"Department":"Arts","course":"B.A. English (Computer Applications)"}, {"Department":"Arts","course":"B.A. Economics"}, {"Department":"Arts","course":"B.A. History"}, {"Department":"Arts","course":"B.A. Political Science"}, {"Department":"Arts","course":" B.A. Tamil  "}, {"Department":"Arts","course":"B.Lit. Tamil  "}]

In my controller, on line 5, I retrieve the value of the clicked item and store it in a variable named "course". I need this variable to properly filter the results in my HTML code. Although I have written filtering syntax in my HTML file, it does not produce the desired result.

Answer №1

Upon further examination, it appears that the .area attribute in engineering.json does not match with the .course attribute in newtab. In each instance of engineering.json, the area is labeled as "Nuclear Medicine Technology Course", resulting in no matches regardless of the department selected from newtab.

You can view a working example on JSFiddle here. I have modified Sam's .area to demonstrate that the filter is functional (and removed extra filters for clarity, although they should still be operative).

An additional issue could be how the data retrieved from $http is typically accessed, as shown below:

$http.get(url).then(function(result) {
    $scope.variable = result.data;
}

This is due to the fact that the object returned by the $http call stores the data within the .data attribute. This is something worth investigating as well.

EDIT: As mentioned by @mzulch in the comments, your $http syntax is accurate for angular versions earlier than 1.5.

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

Tips for displaying an image using the image tag in jQuery

I am looking to dynamically append a share button image after every image tag that meets certain criteria. For the code snippet and demonstration, you can visit this fiddler link here. $(document).ready(function() { $("img").each(function() ...

The breeze is puzzled by the altered being, unable to identify it

I am currently working on a breeze implementation that involves displaying properties from a location object on the UI. However, when I make changes to some properties and attempt to save them, breeze does not recognize the entity as being changed. Here is ...

Error: The database query returned duplicate results while using ngRepeat

I have encountered a persistent error that seems to be related to the "fetchCourses()" function in courses.js. Despite various attempts, the error persists and only goes away when I comment out this particular function. My suspicion is on the HTTP request, ...

Code - Capture user's input

I have multiple input fields in my HTML document and I want to retrieve the text entered into one of them. Here's an example of one such input field: <TH> <FORM> <input name="designation" type="text" size="12" /> < ...

Retrieve the Vue.js JavaScript file from the designated static directory

This is my inaugural attempt at creating a web application, so I am venturing into Vue.js Javascript programming as a newcomer. I have chosen to work with the Beagle Bootstrap template. Within my Static folder, I have a file named app-charts-morris.js whi ...

What is the best way to flatten a nested list within a JSON object?

I have extracted sleep data from my Fitbit and obtained a json file containing nested variables + dict. I am looking to convert this json file into a csv file that will showcase all the "regular" variables, such as "dateOfSleep", as well as the nested vari ...

"Utilize the await/async and promise functions to pause and wait for a code to

I am facing an issue with using await/async in conjunction with Promise.all to retrieve arrays and proceed with the code. However, when I introduce a delay in the code, it seems like it's not functioning as expected. Below is my code snippet: asyn ...

Tips for efficiently organizing and maintaining a selection of JavaScript variables by easily adding and removing them as needed

I am currently developing items that will be shown on a webpage. I achieve this by utilizing the following code: var popup = "something..." Is there a method to keep track of all the created popup variables and control them efficiently using: adding a ...

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

Iterate over the array and show the elements only when a click event occurs

I am trying to create a loop through an array (array) and display the elements one by one only after clicking a button (bt). However, when I run this code, it only shows the last element of the array (i.e. honda). Can someone please help me fix this issu ...

Issue with Angular UI tooltip not closing correctly inside ng-repeat loop

Check out the plunker link provided below for reference: http://plnkr.co/edit/RPpjULZsSDnTFPKiafl2 Essentially, I am experiencing an issue where the angular-ui tooltip persists even when moving to a position with ng-disabled set to true. Any suggestions ...

Showing information in a modal dialog in an Angular 4 application

As an Angular 4 developer, I am working on an application where I need to display data in a dialog. To achieve this, I am using @Output to pass data from the child component to the parent component. In the parent component, my code looks like this: expor ...

Can you explain the purpose of the square brackets within the ".module("modulename", [...])" syntax used in AngularJS?

I recently came across a sample demonstrating routing in AngularJS. I am curious about the connection between the dependency 'ngRoute' and the module mainApp, as shown in the syntax var mainApp = angular.module("mainApp", ['ngRoute']);. ...

Generate a form using code that has the trigger turned off by default

Summary: Looking for a solution to avoid the automatic disabling of a programmatically created trigger in a form. function autoTrigger(e) { var result = {}; var responses = e.response.getItemResponses(); for (var i = 0; i < responses.length; i++) ...

% unable to display on tooltip pie chart in highcharts angular js

https://i.stack.imgur.com/Ccd7h.png The % symbol isn't displaying correctly in the highcharts ageData = { chartConfig: { options: { chart: { type: 'pie', width: 275, height: 220, marginTop: 70 ...

Submitting the form may cause disruptions for others

I currently have an email subscription form for my newsletter that is managed through PHP. This form appears in the footer of every page on my website. Check out a demonstration on JSFIDDLE While the form itself functions properly, I am encountering issu ...

Unable to retrieve value - angularJS

An AngularJS application has been developed to dynamically display specific values in an HTML table. The table consists of six columns, where three (Work Name, Team Name, Place Name) are fixed statically, and the remaining three columns (Service One, Servi ...

Tips for interacting with a custom web component using Selenium WebDriver

As a newcomer to writing selenium tests, I am attempting to create an automated test for a carousel feature on our homepage. The objective is to click on one of the carousel navigation buttons and then confirm that a specific inline style has been applied ...

The architecture of Angular controllers

Being a novice in AngularJs, I have a query regarding the controller structure. This particular file is my employeeController.js (function() { angular.module('employeeApp').controller('employeeController', employeeCont ...