ng-binding does not sync with input field

Check out this JavaScript snippet

angular.module('myApp')
    .controller('HeaderCtrl', function($scope) {
        $scope.searchBooking = "test";

        $scope.goToBooking = function() {
            console.log($scope.searchBooking);
            //$location.path('/bookings/' + $scope.searchBooking);
            //delete($scope.searchBooking);
        }

        $scope.printValue = function() {
            console.log($scope.searchBooking);
        }
    }
);

Here's the corresponding HTML

<div class="input-group custom-search-form">
    <input type="text" class="form-control" placeholder="Testing" ng-model="searchBooking" ng-change="printValue()">
    <span class="input-group-btn">
        <button class="btn btn-default" type="button">
            <i class="fa fa-search"></i>
        </button>
    </span>
</div>

Upon page load, the text field displays "test," but when the field is altered or the button is clicked, nothing happens (console logs "test" with each change or button press). Any ideas on what may be causing this issue?

Answer №1

Consider encapsulating the value within an object. Objects are passed by reference, while simple values are passed by copy. I've encountered this issue multiple times as well.

Here is a suggested approach:

$scope.myObject.searchBooking = "test"

With this modification, the complete code will appear as follows:

angular.module('myApp')
.controller('HeaderCtrl', function($scope) {
    $scope.myObject.searchBooking = "test";

    $scope.goToBooking = function() {
        console.log($scope.myObject.searchBooking);
        //$location.path('/bookings/' + $scope.myObject.searchBooking);
        //delete($scope.myObject.searchBooking);
    }

    $scope.printValue = function() {
        console.log($scope.myObject.searchBooking);
    }
}
);

Additionally, here is the corresponding HTML:

<input type="text" class="form-control" placeholder="Testing" ng-model="myObject.searchBooking" ng-change="printValue()">

Answer №2

Discover this JSFiddle showcasing ng-model binding in ng-change.

HTML

<div ng-controller="MyCtrl">
 <div class="input-group custom-search-form">
    <input type="text" class="form-control" placeholder="Testing" ng-model="searchBooking" ng-change="printValue()">
    <span class="input-group-btn">
        <button class="btn btn-default" type="button">
            <i class="fa fa-search"></i>
        </button>
    </span>
</div>
</div>

Angular controller

 var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {

   $scope.searchBooking = "test";

        $scope.goToBooking = function() {
            console.log($scope.searchBooking);
            //$location.path('/bookings/' + $scope.searchBooking);
            //delete($scope.searchBooking);
        }

        $scope.printValue = function() {
            console.log($scope.searchBooking);
        }
}

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 callback function does not seem to work when using MUI v4 PropFunc

Encountering an issue with custom styling using PropFunc in Material UI (MUI) v4 (4.12.4). When providing a PropFunc in the traditional callback way to get CSS, it works as expected: const useStyles = makeStyles((theme) => { return { input: { ...

Margins are added to cards on the right side

Looking for some help to improve the display of three cards in a grid. The card media has a max-width of 345px, but this is causing excessive margin-right space and making the styling look poorly. Any suggestions on how to eliminate this extra margin? If ...

Monitor user activity for when they click the back button in their browser

Is there a way to detect when the user clicks on the back button of the browser? I'm working on an Ajax application and being able to detect back button clicks would allow me to display the appropriate data. I am open to solutions in PHP, JavaScript ...

Error: Uncaught [🍍]: The function "getActivePinia()" was invoked without an active Pinia instance present. Ensure that you have called "app.use(pinia)" before attempting to utilize a store

I encountered an issue while trying to access a store in Pinia for my Vue web application. Despite installing Pinia and setting app.use(createPinia()), I keep receiving the following error message: Uncaught Error: [ ...

What is the best way to display items using Typeahead.js in combination with the Bloodhound engine?

I am struggling to showcase a collection of items using typeahead with a JSON file as the data source. Unfortunately, none of my information is appearing on the screen. My objective is to list the names and utilize the other attributes for different purpo ...

Switching from JavaScript to TypeScript resulted in React context not being located in its respective file

I previously had my context and context provider set up in a file, and everything was working perfectly. However, I recently decided to convert all of my files to TypeScript, including this one. Unfortunately, I've encountered a strange issue that I c ...

Why is this basic HTML code not functioning as expected?

I attempted to combine HTML and JS to change the color of a box upon clicking it, but after reviewing the code multiple times, I am unable to pinpoint the issue. <!doctype html> <html> <head> </head> <body> <d ...

Setting up Webpack to compile without reliance on external modules: A step-by-step guide

I am facing an issue with a third-party library that needs to be included in my TypeScript project. The library is added to the application through a CDN path in the HTML file, and it exports a window variable that is used in the code. Unfortunately, this ...

loading a module's dependencies seamlessly with RequireJS

Currently, I am working with Knockout and Require in my project. I have isolated some Knockout handlers into a separate module that I want to utilize. While there is no specific JavaScript code relying on this module, it is referenced in the data-bind attr ...

Utilizing a combination of MVC, jQuery, and Ajax to ensure that JavaScript is fully loaded before proceeding

Utilizing ASP.NET MVC and jQuery, I am loading a PartialView via Ajax which has its own accompanying JavaScript file. Upon successful retrieval of the html content, it is inserted into the DOM. However, there can be a delay between the insertion and the ex ...

having difficulty implementing the blur effect in reactJS

Currently, I am working on developing a login page for my project. Below is the code snippet I have implemented for the functionality: import React, { useState, createRef } from "react"; import { Spinner } from "react-bootstrap"; import ...

Guide to building a straightforward line graph using JSON data from a server

When I retrieve price change data from an API endpoint using axios, the response I receive is in the following format: 0: (2) [1639382650242, 48759.49757943229] 1: (2) [1639386250855, 49131.24712464529] 2: (2) [1639389730718, 48952.65930253478] 3: (2) [163 ...

"AngularJS makes it easy for the logged-in user's information to be accessible and available across

I need to ensure that user information is accessible across all views after logging in. How can I adjust the code to be able to retrieve the pseudonym from another view? Could you provide an example as well? Below is my login controller: app.controller ...

Form values not being transmitted correctly in Ajax POST request

I'm facing an issue with my form submission through AJAX where the values are coming out incorrect. Strangely, when I use a normal POST submit it works perfectly fine! <script type="text/javascript"> $(function () { $('#B ...

How should one properly utilize the app and pages directories in a next.js project?

For my current next.js 13 project, I have decided to utilize the /pages directory instead of /app. Nonetheless, I recently included the app directory specifically for its new features related to dynamic sitemap rendering. Do you think this approach is app ...

Trouble with React Native ListView Item Visibility

I'm currently working on integrating the React Native <ListView /> component with the <List /> and <ListItem /> components from React Native Elements. However, I seem to be facing an issue where the <ListItem /> component is no ...

Steps for including a subdocument within a mongoose schema

I am currently working on setting up a subdocument within a mongoose schema using node.js/Express. There are two schemas in play: Member and Address Member.js // app/models/member.js // Loading mongoose to define the model var mongoose = require(' ...

Leveraging ng-repeat with nested JSON data, including JSON within JSON

I'm fairly new to working with Angular and I've run into an issue involving JSON data and ng-repeats. My specific problem involves a list of "modules" with nested lists of "weeks" within them: { "modules": { "module1": ...

Tips for creating multiple popups using a single line of JavaScript code

I am new to JavaScript and I am attempting to create a popup. However, I am facing an issue in opening two divs with a single line of JavaScript code. Only one div opens while the other remains closed despite trying various solutions found on this website. ...

Dynamically parallelizing functions with async and arrays

I have recently integrated the fantastic "async" module by caolan into my Node.js project: Below is a snippet of the code in question: exports.manageComments = function(req, res) { var toDeleteIds = []; var deleteFunctions = []; if (req.body. ...