Trouble with data binding in AngularJS when using the input element

I am encountering an issue with my AngularJS code. When I try to input text into the textbox, it is not appearing in the binding.

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="js/angular.js"></script>
    <script type="text/javascript">
    var app = angular.module('myApp', []);
    app.controller('cont', ['$scope', function($scope) {
    }]);
    </script>
</head>
<body ng-app="myApp">
    <input type="text" ng-bind="user"></input>
    <p>{{user}}</p>
</body>
</html>

Answer №1

If you want to achieve two-way binding, make sure to utilize the ng-model directive. The ng-bind directive only facilitates one-way data synchronization, updating the element with the model value during a digest cycle. It is essential to employ the directive definition for form controls like input, which relies on the optional ng-model directive for functionality.

The element directives, including events such as change/input, are registered based on the presence of the optional ng-model controller on the target element. This controller handles the view-value, model-value, and triggers the digest cycle upon specific events. Additionally, recent versions of Angular offer ng-model-options for specifying the timing of model updates and form validation at either the element or global level.

To implement this approach, consider the following:

<input type="text" ng-model="user" name="user"></input>
<p>{{user}}</p>

While your current setup functions correctly, it's recommended to include ng-controller="cont" usage to prevent all properties from being attached to the rootScope. A revised structure might look like:

<body ng-app="myApp">
  <div ng-controller="cont">
    <input type="text" ng-model="user" name="user"></input>
    <p>{{user}}</p>
  </div>
</body>

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

When using a Vue.js component, the value of this.$route can sometimes come back

I am attempting to retrieve the parameters from the URL and pass them into a method within a Vue component. Despite following advice to use this.$route, I am consistently getting an 'undefined' response. I have tried various solutions suggested ...

XMLHttpRequest request shows blank result

Issue: After clicking the submit button on my HTML form, a JavaScript function is called with an Ajax request. The request returns successfully, but the result disappears quickly. I'm curious if I may be overlooking something here (besides jQuery, w ...

Ways to retrieve the path of a button found within table cells

https://i.stack.imgur.com/pUYHZ.jpgI'm currently working on a table where I've created a button that's being used in various rows and tables based on certain conditions. There's a specific scenario where I need to display the button for ...

Leverage the Ajax response to bypass a loop iteration in Jquery

I am currently facing a challenge where I need to skip a loop iteration based on the response received from an Ajax call. The issue lies in the fact that I am unable to access the Ajax response outside of the actual Ajax call. While I have managed to fin ...

What is the best way to manage a group of checkboxes using EJS?

I am currently utilizing ejs for my website pages and on one particular page, I have an array of objects. The number of objects in the array is not known when the page initially loads. This page allows me to edit a series of announcements and I would like ...

Prevent event propagation in jQuery by using .stopPropagation() when hovering over a

When trying to implement event.stopPropagation() in a specific scenario, I encountered an issue with a blinking background image on my submenu. To address this, I added a pseudo-element (background:green) to the parent element by toggling a new class using ...

I am unable to utilize third-party components within my Nuxt.js/vue.js project

I am attempting to use a library for my Nuxt project, following the guidelines laid out in the documentation available here: getting-started Despite following the instructions provided, I keep encountering errors such as "Unknown custom element: - did you ...

Leveraging the outcome of an API request with Protractor

I have developed a small API that generates test data instantly. Each request creates a new user and provides the relevant data. For fetching the data, I utilize the 'request' package: var flow = protractor.promise.controlFlow(); var result = f ...

In order to display the particle-slider logo effect, the JavaScript on the page needs to be refreshed

I have a website frontend integrated from WordPress using an HTML 5 Blank Child Theme. The site features a logo effect utilizing particle slider for screen sizes greater than 960px, and a flat logo image for screen sizes less than 960px. Everything works p ...

Using Typescript with d3 Library in Power BI

Creating d3.axis() or any other d3 object in typescript for a Power BI custom visual and ensuring it displays on the screen - how can this be achieved? ...

By-passing Mistakes in Iteration in JavaScript

I have been working on using Google's Feed API to fetch images from a feed within an AngularJS controller. It successfully retrieves three images, but encounters an issue when it reaches a Twitter URL, causing the script to break. I would like it to s ...

What is the best way to make a JavaScript cookie remember the state of a DIV?

Seeking assistance with a test site here that is currently in development. I am attempting to make the notification at the top remain hidden once the close button is clicked. Below is my current script: <style type="text/css"> <!-- .hide { displ ...

The architecture of Angular controllers

Being a novice in AngularJs, I have a query regarding the controller structure. This particular file is my employeeController.js (function() { angular.module('employeeApp').controller('employeeController', employeeCont ...

Utilizing Vue.js to add functionality for navigation buttons allowing users to move between survey questions

In my Vue.js component, I've written code to show survey questions in a mobile app for users. Here is a snippet of the code: <div class="col-12 p-0" v-for="( i, index ) in questions" :key="i"> <p cl ...

Performing mathematical computations through a mixin without relying on inline javascript code

Looking to enhance my web app with a mixin that shows a list of entries, each with a time stamp indicating how long ago it was posted. mixin listTitles(titles) each title in titles article.leaf article a.headline(href=title.URL)= title.t ...

Trigger event once item is selected from the jQuery combobox dropdown

I have implemented a custom autocomplete combobox using the jQuery UI library to create text fields with dropdown functionality. This hybrid input allows users to either select an item from the dropdown or enter free text manually. However, I need to trigg ...

``motioning a component beneath another component that may be in a state

Having an issue with my CSS. I have some elements generated by JavaScript and when hovering over them, another element is displayed below the others for some reason. Here's the CSS related to this problem: .hiddenTextjob { display:none; ...

When implementing variables from input boxes, my SQL query fails to populate any data in the database table

I have been using phpMyAdmin to store test data. As I try to insert data from a form, I encounter an issue where no data gets inserted when using variables in the SQL query. Being new to coding, I am struggling to find a solution to this problem. Additiona ...

Failed to fetch http://localhost:8000/Stack/script.js due to network error 404 in Django project

As I embark on creating a simple to-do app, I encountered an error while trying to implement some JavaScript on the homepage. The error in question is highlighted above (Get http://localhost:8000/Stack/script.js net:: Err_Aborted 404). Being new to integra ...

AngularJS ng-repeat along with jQuery Mobile

I am attempting to utilize ng-repeat in combination with jQuery mobile checkboxes. While the checkboxes appear correctly, they do not become checked when clicked. Could someone provide guidance on how to effectively use ng-repeat with jQuery mobile checkb ...