Unable to retrieve the selected name in the controller

Forgive me for this, but I am still learning angularjs.

I am facing an issue with a select control that is populated using the following code:

<select ng-model="limit" data-ng-options="listLimit.name for listLimit in listLimits" data-ng-change="limitChanged()"></select>

After the control is set up, the options are simply 50, 100 & 150.

The problem arises when I try to change the value in the select control. It seems like the limitChanged() function does not recognize the newly selected option. Here is an example:

module.controller('PageController', function ($scope) {
    
    $scope.limit = $scope.listLimits[0]; // $scope.listLimits[0] is essentially {name:50}

    $scope.limitChanged = function() {
        console.log($scope.limit.name); // This will log 50 (the default value) regardless of the selected option
        loadList(); // This function changes the item limit
    };
}

However, when I use {{limit}} in the view, it does display the updated value when the select control is changed. I am a bit confused. Can anyone help me understand what I might be doing wrong?

Answer №1

There could be a potential issue with your data in the listLimits variable, but it seems to be working fine for me.
Check out this jsFiddle example

function ParentCtrl($scope) {

     $scope.limitList =[
         { name: "value1"},
         { name: "value2"},
          { name: "value3"}
     ];

    $scope.limit='';

    $scope.limitChanged=function(){
      console.log($scope.limit);
    }

}

Answer №2

It appears that the issue stemmed from the absence of the ng-controller attribute within the enclosing div. This led to the control not being included in the controller's scope of actions.

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

Initializing an HTML5 video player directly from an Array

I am trying to populate a video player on my webpage with an array of videos. Here is the code I have so far: var current = 0; var videos = ["01", "02", "03", "04"]; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomInde ...

JS - reloading the location after a function has been carried out with a short delay

Currently, I am using a JS / AJAX script to update information in my table. After the script finishes running, I want to reload the page. Unfortunately, when using my current code, the page reloads instantly. Is there a way to add a delay of 3 seconds befo ...

Ending a timed function in AngularJS 1

As part of my Angular JS 1 learning journey, I am working on a small test involving text areas that display text using Angular functions when a user enters and exits them. The enter function has a 3-second delay, while the exit function waits for 5 seconds ...

Navigating through the sidebar on Next.js

I am currently exploring how to utilize next.js routes for dynamically populating a sidebar component with route instructions. The goal is to have the main div display the relevant component when a sidebar option is clicked. While I've come across tu ...

Techniques for sending a list of objects to a method using AJAX in MVC

Hey there, I am facing an issue with passing a list in an ajax call to my controller as I am getting null data. Below is the code snippet: Class: public class CurrentProcessNameAndBucketName { public string ProcessName { get; set; } public string ...

Performing a function multiple times by clicking the mouse

I have a function called week(), which provides me with the current first (startDate) and last (endDate) day of the week along with the week number. Additionally, there are two other functions, namely weekPlus() and weekMinus(), containing variables that i ...

Need help with setting up a variable?

I'm having trouble figuring out how to store the value returned from a stored procedure in web API. How can I pass variables through AngularJS? I made a service call var promisePost = crudService.candidatePost(Candidates); promisePost.th ...

Rdash Angular: How to Implement a Fixed Header Position on Page Scroll

Hi there, I am currently utilizing Rdash Angular Js. I am wondering if it's achievable to keep the header position fixed while scrolling through the page. I attempted a solution using CSS as shown below, unfortunately it didn't have the desired ...

angular $routeSegmentProvider navigate through route paths

When utilizing $routeSegmentProvider, I inject $routeSegment into my controller. This provides information about the route used to construct the current page (chain property). To further enhance my service, I am searching for a method to generate a compl ...

Unable to identify the origin of the ng-change runtime error

I encountered a compile error at runtime when running the code below. Despite my efforts, I am unable to pinpoint the exact issue related to my usage of ng-change: <!doctype html> <html> <head> <script src="https://ajax.googleap ...

What is the most efficient way to find the sum of duplicates in an array based on two different properties and then return the

var data = [ { "amount": 270, "xlabel": "25-31/10", "datestatus": "past", "color": "#E74C3C", "y": 270, "date": "2020-10-31T00:00:00Z", "entityId": 1, "entityName": "Lenovo HK", "bankName": "BNP Paribas Bank", "b ...

Generate a loop specifically designed to execute the code following the menu in the script

My website has the code snippet below: let txt_com = document.querySelector(".text_user"); let num_com_user = document.querySelector(".massage_for_user"); txt_com.addEventListener("click", function() { if (this.classList.contains("num_com_user")) { ...

Finding the index number of a DIV element when it is clicked

Here is an example of the HTML structure I am working with: <div id="preview-wrapper"> <div class="dz-preview dz-image-preview"> <a class="rotate-a" href="javascript:void(0);"> <img class="rotate" src="public/a ...

Issue: The component factory for GoogleCardLayout2 cannot be located

Recently I've been working on an Ionic 3 with Angular 6 template app where I encountered an issue while trying to redirect the user to another page upon click. The error message that keeps popping up is: Uncaught (in promise): Error: No component fac ...

The function is not being called by ngModelController $parsers in Angular version 1.4.7

I'm encountering an issue with ngModel.NgModelController.$parsers. Let me explain with an example: I have created a directive for an input field. Whenever I change the value in this input in the HTML, it should call my specified function. The registe ...

Configuring headless unit testing with requirejs

Seeking a JavaScript unit testing environment, I feel like I'm on a quest for the Holy Grail. The criteria are as follows: testing Requirejs AMD modules isolating each module by mocking out dependencies ability to test in-browser during development ...

Using Three.js: Cloning Mesh and Material to Easily Toggle Clone Opacity

By using the command Mesh.clone();, it is possible to duplicate the mesh. Upon further investigation, I discovered that both the geometry and material are preserved in the clone. However, my goal is to independently adjust the opacity of each mesh. This le ...

Yii employs the use of quickdlgs to efficiently generate fresh entries within the CGrid view

Within my web application, I am attempting to implement an iframe button using quickdlgs to create new records. However, upon clicking the iframe button, instead of being directed to the 'create' webpage, I am presented with an empty iframe. Here ...

PHP Foreach Loop doesn't execute JavaScript Sum Function as expected

Currently, I have a JavaScript function implemented that utilizes the IDs of 3 textboxes to retrieve their numeric values, sum them together, and display the result in the fourth textbox. In addition, I am fetching a list of individuals from my database an ...

Purging browser cache with the help of jQuery or AngularJS

I am currently working on clearing the browser cache through programming. This is necessary because I have updated the application to a new version, but the browser continues to display the old version and content stored in the cache. I want to ensure that ...