The Angular directive ng-model is not able to return a value

I'm currently troubleshooting an issue with the filters in an older project. Here's the HTML snippet:

<input type="text" class="form-control" ng-model="FilterEventsEdit"
       ng-change="FilterEvents()"
       style="width: 300px; margin-bottom: 5px; display: inline;">

And now onto the JavaScript:

$scope.FilterEvents = function () {
    $scope.EventsGridDataSource.filter({
        logic: "or",
        filters: [
            { field: "Name", operator: "contains",
              value: $scope.FilterEventsEdit },
            { field: "Expression", operator: "contains",
              value: $scope.FilterEventsEdit }
        ]
    });
};

Despite having a similar setup that works with different models, I keep getting $scope.FilterEventsEdit as undefined. The problem has me stumped.

Answer №1

Make sure to set the $scope.FilterEventsEdit variable before attempting to use it.

$scope.FilterEventsEdit = "";

$scope.FilterListEvents = function () {
    $scope.EventGridDataSource.filter({
        logic: "or",
        filters: [
            { field: "Title", operator: "contains", value: $scope.FilterEditEvents },
            { field: "Description", operator: "contains", value: $scope.FilterEditEvents }
        ]
    });
};

Answer №2

When using ng-model in your controller, you are able to retrieve the value of the text box correctly. The event is triggered when any input is entered into the text box. However, it's important to check the initial value of $scope.FilterEventsEdit when the page loads. If it hasn't been set, it defaults to undefined. Your method is currently only called when the input text changes. I conducted a test on a sample JS fiddle and everything seemed to be functioning properly. Here is a snippet of the code:

<input type="text" class="form-control" ng-model="FilterEventsEdit" ng-change="FilterEvents()" style="width: 300px; margin-bottom: 5px; display: inline;">

// Setting empty string as default. Adjust this based on your requirements
$scope.FilterEventsEdit = '';
// When not explicitly assigned, console will log 'undefined'
console.log('filterEventsEdit: ', $scope.FilterEventsEdit);

$scope.FilterEvents = function () {
    console.log($scope.FilterEventsEdit);
    /* Commenting out the filter logic for testing purposes
     $scope.EventsGridDataSource.filter({
        logic: "or",
        filters: [
          { field: "Name", operator: "contains", value: $scope.FilterEventsEdit },
          { field:"Expression", operator: "contains", value: $scope.FilterEventsEdit}
        ]
    });*/
};

The method will also display the current value of the text box. For a demonstration, feel free to experiment with this working JS fiddle: https://jsfiddle.net/sagarag05/4jb1cpog/6/

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 D3 force layout is currently displaying just a single connection

I've been experimenting with a graph that I found in this demo and now I want to modify it to display data from Spotify. I created a sample JSON file and adjusted the script accordingly for the new data format, everything seems to be working fine exc ...

Having trouble loading the React app when using the @mui/styles package

After downloading a package from the MUI website, I encountered an error when trying to import "@mui/styles" which is resulting in browser errors. The package was downloaded from this source: https://mui.com/system/styles/basics/ Upon running my React app ...

"Introducing a smooth animation effect to shift text alignment to the center

I'm currently using the code below, but I am not satisfied with the margin adjustment achieved by text-align:center;. Is there a way to smoothly transition the text alignment to center similar to how it is done in the following snippet of code? if ($ ...

What value does a variable have by default when assigned to the ko.observable() function?

I am in the process of learning KnockoutJS and I have implemented code for folder navigation in a webmail client. In the view code, there is a comparison being made to check if the reference variable $data and $root.chosenFolderId() point to the same memor ...

Passing data from a child component to a parent component in Vue 3: How

Struggling with Vue 3 app authentication through event-emission from child to parent. Below is a snippet of the code: Child <template> <form class="msform"> <input @click="goToLogin" type="button" name=&q ...

Incorporate JSON data using jQuery's AJAX in MVC3

I need assistance with parsing the JSON data retrieved from a webservice through my controller. Currently, I am displaying the entire JSON string in a div as text. However, I only want to extract specific values such as "color" and "size". I am unsure of ...

Using Angular and Jasmine: techniques for simulating a service that provides a promise

In my AngularJS application, I have a controller called MenuCtrl that utilizes a service provided by "$mdSidenav" from Angular Material. This service is created using a factory method. angular.module('leopDirective', []) .controller('Me ...

Creating Vue Components in PHP with no need for Webpack or Bundlers

Our company is currently in the process of incorporating Vue into our workflow, but we are facing challenges due to the lack of Javascript Build tools like Webpack. This means we cannot use Babel or Template Literals (`), making it difficult to define Comp ...

Using the timer function to extract data within a specific time frame - a step-by-step guide

Is there anything else I need to consider when the temperature increases by 1 degree? My plan is to extract data from my machine for the last 30 seconds and then send it to my database. set interval(function x(){ If(current_temp != prev_temp){ if((c ...

Tips for disabling autofocus on textarea when it is initially created in Internet Explorer browser

By clicking a button, I can generate a new <textarea></textarea>. However, in Internet Explorer, the cursor automatically moves to the newly created text area. Is there a way to prevent this from happening? ...

Restrict dropping items in HTML5 by only allowing the drop if the target div is

I am working on developing a user-friendly visual interface for creating simple graphics. The interface includes 10 image icons and 5 boxes where users can place these icons. Users have the freedom to select which icon they want to display and arrange them ...

What is the best way to adjust the placement of a component to remain in sync with the v-model it is connected to?

I am encountering an issue with 2 sliders in my project. I have set it up so that when the lower slider's value is greater than 0, the top slider should automatically be set to 5. I am using a watcher function for this purpose. However, if I manually ...

Perform a replacement with jQuery.replaceWith() on an input field and refocus in Microsoft Internet Explorer

There exists a script http://gist.github.com/457324 which establishes default text (extracted from the element's title attribute) for empty input[type=text] as well as input[type=password] elements. However, setting default text for password elemen ...

Having difficulty accessing node_modules directory in JavaScript

I am confused about how to access node_modules for JavaScript. Can someone provide an example of calling module.exports from a node_module folder after installing a package with NPM in Node.js? File tree structure: There is a folder named 'ethereum&a ...

What is the best way to access query string parameters within NextJS middleware?

In the context of NextJS middleware, I have successfully obtained the nextUrl object from the request, which includes details like the pathname. However, I am now wondering how to extract query string parameters directly within the middleware. Although I c ...

Embed an angular directive into HTML dynamically

I have a multitude of directives within my angular module, ranging from MyDirective1 to MyDirectiveN. Furthermore, I keep track of these directive names in MyDirectiveList: ['MyDirective1', 'MyDirective2', ..., 'MyDirectiveN' ...

What is the best way to implement a dropdown menu with JavaScript or jQuery?

I'm looking to create a dynamic dropdown list using Javascript or JQuery that mirrors the functionality of selecting countries, states, and cities. Can someone provide guidance on the best code to achieve this? Below is a snippet of my existing code: ...

Angular-Phonegap - The issue with "min-height: 100%" not functioning properly

I am faced with this specific HTML file: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <!-- NOTE: Make sure to remove the width=d ...

Editing rows directly within the ng table interface

Hey there, I'm currently working on implementing inline row editing using ng table After clicking the edit icon and changing values, I encounter an error upon clicking the save icon: TypeError: Cannot read property 'untrack' of undefined ...

Unexpected element layout issues

Attempting to create a basic website that utilizes the flickr api, retrieves photos and details, and displays them using bootstrap. However, there seems to be an issue that I am unsure how to resolve. Currently, my code is functioning like so: https://i.s ...