Struggling to get the AngularJS filter to function properly when using a $scope

In my AngularJS application, I have a requirement to display courses and their equivalent courses in a table. The table should dynamically update based on the selection made from a drop-down menu.

HTML

<form name = "Select University" ng-show = "courseType == 'extern'">
    <label for = "University"> University: </label>
    <select name = "University" id = "repeatSelect" ng-change = "changeIt()"ng-model = "univs.repeatSelect">
            <option ng-repeat = "univ in univs.uniNames | orderBy: 'School'" > {{univ.School}} </option>
    </select>
    </form>



 Courses at {{univs.repeatSelect}}
    <table>
            <tr>
            <th> Course </th>
            <th> Equivelance </th>
            </tr>

            <tr ng-repeat="x in names | orderBy: 'className' | filter:univs.repeatSelect">
            <td>{{ x.className }}</td>
            <td> {{x.equiv}} </td>
            </tr>
    </table>

JavaScript

$http.get("http://linux.students.engr.scu.edu/~cmulloy/COEN174/getSchools.php")
    .success(function (response) 
    {
            $scope.univs.uniNames = response.records;
    });

$scope.univs = 
    {
            repeatSelect: null,
            uniNames: null,
    }; 

The scope variable {{univs.repeatSelect}} is displaying correctly on the page. However, when I use it as the filter argument in ng-repeat, the table remains empty. When I manually input 'UCLA' as the filter argument, it works fine. Am I overlooking something here?

Answer №1

It is recommended to include a value attribute in your select options, which will then assign the value of the value attribute to the corresponding ng-model.

<select name = "University" id = "repeatSelect" ng-change="changeIt()" ng-model="univs.repeatSelect">
    <option ng-repeat="univ in univs.uniNames | orderBy: 'School'" value="{{niv.School}}"> {{univ.School}} </option>
</select>

A more optimal approach would be to utilize ng-options when working with a select box.

<select name="University" id="repeatSelect" 
   ng-change="changeIt()" ng-model="univs.repeatSelect" 
   ng-options="univ.School as univ.School in univs.uniNames | orderBy: 'School'">
</select>

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

connect input field with dropdown menu

Check out my Demo. I am looking to link a text box with a select list, so when I type "vahid" in the textbox, the value changes to vahid in the select list. $scope.options2 = [{ name: 'omid', value: 'something-about-ali' }, ...

Tips for iterating through data in JSON format and displaying it in a Codeigniter 4 view using foreach

As a newcomer to JSON, I have a question - how can I iterate through JSON data (which includes object data and object array data) using jQuery/javascript that is retrieved from an AJAX response? To illustrate, here is an example of the JSON data: { "p ...

Utilizing AngularJS: Techniques for Binding Data from Dynamic URLs

Just starting out with AngularJS We are using REST APIs to access our data /shop/2/mum This URL returns JSON which can be bound like so: function ec2controller($scope, $http){ $http.get("/shop/2/mum") .success(function(data){ ...

error is not defined in the onsuccess function of the ajax.beginform partial view

Currently, I am working on an MVC5 project where a View is calling a Partial View. Within the Partial View, there is an Ajax.BeginForm that triggers a function on OnSuccess. However, during execution, I encounter an error stating that the function cannot ...

Issues with npm @material-ui/core Button and TextField functionality causing errors and failures

I'm currently working on a React project and decided to incorporate the material-ui framework. After creating a component that includes a textfield and a button, I've encountered an issue where I can't interact with either of them as they s ...

Is Riot.js the best choice for server-side rendering and routing?

Currently, I am using Node along with Riot.js and the Grapnel routing library. I have successfully set up routing on the client side, but I am facing difficulty in making it work on the server side. The functioning of my client router is straightforward. ...

Tips for updating the appearance of a ListItem with a click action

For my current project, I am using Material UI and React. One of my components is structured as follows: import React, { Component } from 'react'; import { List, ListItem } from 'material-ui'; import PropTypes from 'prop-types&apo ...

Implementing SVG in NextJS 13 with custom app directory: A step-by-step guide

Recently, I decided to explore the app directory and unfortunately ran into some issues. One of the main problems I encountered was with image imports. While PNG images imported without any problem, SVG images seemed to break when importing in /app. For i ...

Is it possible to enable unknown keys for multiple schema objects simultaneously using Joi Validation?

I have a multitude of validator files each containing nearly a hundred schema objects. My goal is to validate unknown keys for all of them simultaneously. I've managed to figure out how to do it for a single object, as shown below. Is there a way to a ...

Dynamic field refreshed on server side upon second button press

I'm encountering an issue where a hidden field that I update via Javascript only reflects the new value after clicking a button twice. Surprisingly, I can view the updated hidden field value when inspecting it through the browser. Default.aspx <s ...

Implementing a change event upon setting a value to an input element using JavaScript

My plan is to develop a Chrome extension that can automatically fill passwords. The code for this functionality looks like the following: //get the account document.querySelector("input[type=text]").addEventListener('input', () => { ...

The initialization of an Angular variable through ng-init will consistently result in it being undefined

Currently, I am in the process of learning AngularJS. As part of my learning journey, I decided to initialize a variable using ng-init. However, I encountered an issue where the variable's value always remained undefined. Below is the snippet of code ...

What could be causing the error "Unexpected identifier 'trytoCatch' while trying to minify?

I recently updated my script.js and now I'm looking to use "minify" in Node.js to compress it. When I type the command minify script.js > script.min.js into the terminal, I get an error message that says: /node_modules/bin/minify.js:3 import "tryTo ...

AngularJS Jasmine test failure: Module instantiation failed

Everything was running smoothly with my angular app and tests using karma and jasmine. However, after adding a dependency in ui.bootstrap, my app continues to function as expected but now my tests won't run. Here is what I have: app.js - added depend ...

Mastering the art of transforming JSON data for crafting an exquisite D3 area chart

I often find myself struggling with data manipulation before using D3 for existing models. My current challenge is figuring out the most efficient way to manipulate data in order to create a basic D3 area chart with a time-based x-axis. Initially, I have a ...

The function app.post in Express Node is not recognized

I decided to organize my routes by creating a new folder called 'routes' and moving all of them out of server.js. In this process, I created a file named 'apis.js' inside the routes folder. However, upon doing so, I encountered an error ...

Issue encountered when making a request from React to Express without using Create React App (CRA)

import React,{Component} from 'react'; import '../styles/App.css'; class App extends Component{ constructor() { super(); this.state = { products: [] }; } componentDidMount() { fetch('http://localhost:40 ...

Guide for dynamically assigning the "required" attribute to a directive template

I am currently working on developing a directive that includes an input. This input has the option to include the required attribute. For example: <my-input name="test" required/> or <my-input name="test" /> Within my directive, the templa ...

Tips for moving an input bar aside to make room for another

Is it feasible to have two input bars next to each other and keep one open until the other is clicked, then the first one closes as well? I attempted using a transition with "autofocus," but the bar ends up closing when clicked on in my site. If anyone ca ...

Avoid using single quotes in Postgres queries for a more secure Node.js application

Snippet from my node js code: var qry = 'INSERT INTO "sma"."RMD"("UserId","Favourite") VALUES (' + req.body.user + ',' + JSON.stringify(req.body.favourite) + ')' My problem is inserting single quotes before JSON.stringify(r ...