There appears to be an unspecified parameter in the controller related to the ng

After fetching data from an API, I use it to populate a form with cascading select fields. I have two functions in place to filter the array based on the selected options, but I am running into an issue where I cannot access properties from ng-model.

For instance, when I select the first option and retrieve its ID, I then need to pass that ID to a function in the controller which filters the array to display the appropriate options.

API DATA

section: { id: "111", name: "Section1" }
nomination: { id: "666", name: "Nomination2" }
category: { id: "999", name: "Category2"}

SERVICE

const allSections = [
    { id: '111', section: 'Section1' },
    { id: '222', section: 'Section2' },
    { id: '333', section: 'Section3' },
];
const allNominations = [
    { id: '555', sectionId: '111', nomination: 'Nomination1' },
    { id: '666', sectionId: '222', nomination: 'Nomination2' },
    { id: '777', sectionId: '333', nomination: 'Nomination3' },
];
const allCategories = [
   { id: '999', sectionId: '111', category: 'Category1' },
   { id: '888', sectionId: '222', category: 'Category2' },
   { id: '000', sectionId: '333', category: 'Category3' },
];

getSection() {
   return allSections;
},
getSectionNomination(sectionId) {
   const nominations = ($filter('filter')(allNominations, { sectionId }));
   return nominations;
},
getNominationCategory(sectionId) {
   const categories = ($filter('filter')(allCategories, { sectionId }));
   return categories;
},

CONTROLLER

$scope.award = {};
$scope.sections = awards_service.getSection();
$scope.getSectionNominations = () => {
    $scope.nominations = 
    awards_service.getSectionNomination($scope.award.section.id);
    $scope.categories = [];
};
$scope.getNominationCategories = () => {
    $scope.categories = 
    awards_service.getNominationCategory($scope.award.section.id);
};

VIEW

<select ng-model="award.section.id"
        ng-change="getSectionNominations()" 
        ng-options="object.id as object.section for object in sections")
</select>

<select ng-model="award.nomination.id"
        ng-change="getNominationCategories()"
        ng-options="object.id as object.section for object in nominations") 
</select>

<select ng-model="award.category.id"
        ng-options="object.id as object.section for object in categories"
</select>

I am facing difficulties in retrieving the selected ID from the API Data and filtering my arrays. Although I can see the correct ID as award.section.id in the view, passing this parameter to the function in my controller results in undefined value. $scope.award.section.id is returning undefined.

Answer №1

Utilize the current select value in a function:

$scope.sectionId = 0;
if ($scope.sections && $scope.sections[0]) {
  $scope.sectionId = $scope.sections[0].id;
}
$scope.getSectionNominations = (sectionId) => {
    if (!sectionId) return;

    $scope.nominations = awards_service.getSectionNomination(sectionId);
    $scope.categories = [];
};

and in html:

<select 
  ng-model="sectionId"
  ng-change="getSectionNominations(sectionId)" 
  ng-options="object.id as object.section for object in sections"
>
</select>

Explore more details here under Pass variable as parameter

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

The exceljs function for downloading an Excel workbook is not activated by using the Post method

Presently, I am engaged in the development of a React and Node application. This app is designed to store data entries in a database, with the capability for users to download all stored data in an Excel workbook. To make this possible, I have integrated ...

What methods are available to create distinctive, shareable links akin to those utilized by Zoom and Google Hangouts?

I'm currently developing a group video chat app and I'm facing the challenge of generating distinct shareable links for every chat room created. Can anyone guide me on how to accomplish this? My aim is for users to easily join the chat room when ...

What exactly is the state ID that xtext employs when saving resources?

I have integrated xtext's orion editor in an iframe within my Angular application. I am facing a challenge where I need to save the edited content from my Angular application to a backend Java application. Can someone guide me on the API calls or appr ...

Enabling a mat-slide-toggle to be automatically set to true using formControl

Is there a way to ensure that the mat-slide-toggle remains true under certain conditions? I am looking for a functionality similar to forcedTrue="someCondition". <mat-slide-toggle formControlName="compression" class="m ...

Issue with grunt-closure-tools: ERROR - There is a parse error due to using a reserved word as an

Currently, I am utilizing the "grunt-closure-tools" tool. Initially, minifying a simple JS file was successful without any issues. However, encountering an exception arose when attempting to minify either the AngularJS or Bootstrap libraries: Error: Comm ...

Tips for disabling autofocus on Mui Menu Items

Incorporating a search functionality within a custom Mui Select component where the user can input a city or country to filter down the options list. The current issue is that when a user types a letter that matches the first letter of an option, the opt ...

Colorful radial spinner bar

I am interested in replicating the effect seen in this video: My goal is to create a spinner with text in the center that changes color, and when the color bar reaches 100%, trigger a specific event. I believe using a plugin would make this task simpler, ...

The most efficient way to invoke a jws void method in the fewest steps possible

When calling a void method of a SOAP JWS web-service using JavaScript without expecting a result back, what is the most efficient and concise approach? ...

JQuery will only run after the Alert has been triggered in Firefox

I am currently facing an issue with updating the 'like' count in my database using the 'changeRating()' method when a user interacts with local like, Facebook like, or Twitter buttons. Oddly enough, the 'likes' are not being ...

What is the best way to split a JSON array into two separate JSON arrays?

Greetings, I have received a JSON response. response.data: [{ "id": 1, "datefrom": "2018-08-30 11:21:25", "dateto": "2018-08-31 11:21:25", }, { "id": 2, "datefrom": "2018-08-30 11:21:25", "dateto": " ...

Refresh a page on Django without the need to reload

Looking to enhance the functionality of my homepage.html by implementing a button action that retrieves server data without causing a full page reload. The project name is "T2KG". The form structure is as follows: <form method="POST> {% csrf ...

Tips for simplifying a JavaScript function

Hello everyone, I just joined StackOverflow and I could really use some assistance with a JavaScript and jQuery issue that I'm facing. Can someone suggest a more efficient way to write the code below?: jQuery(document).ready(function () { $("#ar ...

Can you explain the purpose of the bindToController function within AngularJS 1.4?

Can you explain the function of bindToController in AngularJS 1.4? Is it possible that this change alters the controller's functionality, making it the source for functions instead of the scope? ...

The issue of Vuetify table failing to scroll in IE11

Having trouble getting Vuetify to function in my Vue project on IE11. I need to make content responsive when it's not available, or add a horizontal scroll bar in IE11 when the content is too wide. [Visit this Codepen for reference][1] [1]: https ...

Setting up Type ORM configuration with a .env file in Nest.JS: A step-by-step guide

I encountered an issue while attempting to configure TypeORM with a .env file and run migration script. Here's what I tried: 1. Added scripts to package.json "migration:generate": "node_modules/.bin/typeorm migration:generate -n", "migratio ...

AngularJS: Setting up filter asynchronously

I'm facing a challenge when trying to set up a filter that requires asynchronous data. The filter's purpose is simple - it needs to convert paths to names, but this task involves a mapping array that must be fetched from the server. While I cou ...

"employing" the extent and characteristics using square brackets for notation

Can we access object properties that can only be accessed with square bracket notation while inside a "with" statement? For instance: var o = { "bad-property": 1, "another:bad:property": 2, "goodProperty": 3 }; with(o) { console.log(goodProperty); / ...

Page loading causing sluggishness in Angular application

I've been encountering this problem for quite some time now and have searched extensively online for solutions. However, I believe I may not be using the correct terminology to accurately pinpoint the issue. Within my package.json, I have included th ...

Dealing with a frustrating roadblock in Three.js where you encounter an "Unknown format" error while trying to work with

Greetings, I am relatively new to THREE.js and currently experimenting with loading a .FBX Object using the FBXLoader found in three/examples/jsm/loaders/FBXLoader while integrating this into React.js. Upon launching the page, I encountered an issue where ...

Exploring the meaning behind RxJS debounce principles

Referencing information found in this source: const debouncedInput = example.debounceTime(5); const subscribe = debouncedInput.subscribe(val => { console.log(`Debounced Input: ${val}`); }); When the first keyup event occurs, will the debouncedI ...