Capturing the value of an input field within a controller in AngularJS

I have been programming with JSP and Angular JS. Currently, I am faced with a scenario where I need to work with a JSP page containing a hidden input field. To set the value of this input field, I am using a session attribute like so:

  String policy = (String)session.getAttribute("POLICY_CHANGE");

  <input type="hidden" value="<%=policy%>" name="policy" ng-model="$scope.policyChange" />

My main challenge is how to bind the value of this input field to a variable $scope.policy within my controller.

JS

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.policyChange= ; // My goal is to assign the input field's value here.
});

Answer №1

If you want to set the ng-model directive for your input, here is how you can do it:

 <input type="hidden" value="<%=policy%>" name="ng2_session" ng-modal="vm.policyChange" ng-model="policy" ng-init="policy='<%=policy%>'" />

In the controller, make sure to use the watch method.

The $watch function allows you to monitor changes within the $scope

Here's a JavaScript example:

 app.controller('myCtrl', function($scope) {
   console.log($scope.policy); // This will display the input field value.
 });

For a simple illustration:

function MyCtrl($scope) {
   $scope.$watch('policy', function() {
      console.log($scope.policy);
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="MyCtrl">
      <input type="text" ng-model="policy" ng-init="policy='Bob'"/>
  </div>
</div>

Important Note: The watch method is particularly useful when you need to execute certain code upon a change in the $scope.variable.

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

Using multiple html files with ui-router's ui-view feature

Is it possible to create a complex UI using multiple HTML files with ui-view in AngularJS? I am trying to achieve a layout similar to this: I attempted to implement something on Plunker, but it seems like I'm struggling to grasp the underlying concep ...

Incorporating image hyperlinks onto a designated webpage within a JavaScript presentation carousel

Working on an e-commerce website, the client has requested 3 slide shows to run simultaneously displaying specials or promotions. I have successfully set up the 3 slide shows next to each other, but I'm unsure how to incorporate image links that direc ...

The ng-blur functionality will only trigger if the click event occurs within the bounds of the textbox

I am facing an issue with a textbox that should display a certain value even after being deleted. When the input is cleared and clicked away from, I want the value to reappear in the textbox. Currently, there seems to be a problem with this functionality ...

Encountering an Uncaught Error in Angular 1.2.9 despite the inclusion of ngRoute module

Even after ensuring the ngRoute file is included and ngRoute is added to dependencyInjection, I am still encountering the following error: Error: [$injector:modulerr] http://errors.angularjs.org/1.2.7/$injector/modulerr?p0=demoModule&p1=Error….org% ...

Extract information from a webpage using Selenium WebDriver

Currently, I am working on mastering Selenium, but I have hit a roadblock that I need assistance with. My task is to gather all the betting information for games from the following link and store it into an array. Given my limited experience with HTML an ...

What is the most effective method for storing an object in React?

I am facing an issue with storing my Object of my Checkout-Cart in a React Variable. I attempted to use the useState Component, but unfortunately, it did not work. Here is the object that I receive from my NodeJs Backend : [ { product_uuid: '3b ...

Retrieve the value of a nested JSON object using the name of an HTML form field, without the use of eval

I am facing a similar issue to Convert an HTML form field to a JSON object with inner objects, but in the opposite direction. Here is the JSON Object response received from the server: { company : "ACME, INC.", contact : { firstname : "Da ...

Developing tests for an asynchronous function

I recently encountered a bug in my AWS Lambda code written in NodeJS 6.10 that caused me two sleepless nights. I didn't conduct integration testing, relying solely on unit tests, which led to the oversight. After inserting return workerCallback(err);, ...

Struggling with fixing a 500 error caused by angular routing? Let us assist you

Struggling with resolving the 500 issue. Unsure if changes need to be made in express or angular. Here is my main.js public code snippet. Any assistance would be greatly appreciated. var app = angular.module('myApp', ['ui.router'], fun ...

Is browserHistory.push ineffective in react.js?

I am currently working on a React ecommerce website. I have encountered an issue in the login component where, upon clicking the submit button on the form, the system is supposed to check if the user is authenticated using useEffect. If the user is authent ...

Leveraging Polymer web components without the need for a package manager

I am looking to incorporate web components into a static web page without the need for any package manager or JavaScript build process. After referencing , I attempted to import them using unpkg by changing <script type="module" src="node_modules/@pol ...

Prevent vertical axis rotation in OrbitControls with THREE.js

In my three.js project, I have a scene with OrbitControls that allows me to rotate the camera around the origin at position 0,0,0. I am attempting to restrict the camera rotation to only rotate vertically along the y-axis infinitely (up or down). You can ...

Implementing automatic form saving in AngularJS through sessionStorage as you enter data

I have been attempting to use sessionStorage for auto-saving form data as it is being entered into an input field. However, I am encountering an issue where the data is not being saved. Ideally, when the page is refreshed, the data should still be present ...

Getting the string value from a table row using JavaScript

I need to capture the value of result_status from the row labeled status. If all values in the row labeled status are 'pass', then the result_status will also be 'pass'. However, if any one of the values in the row labeled status is &a ...

Transforming precise military time using Angular and Javascript filtering

There are a few times where I find myself liking 01:45 //and 15:00 I believe this time format is HH:MM in military time? While I have come across some advanced functions that can parse sentences and even include seconds like HH:MM:SS, I am looking for a ...

before sending the url with fetch(url), it is adjusted

My front end code is currently fetching a URL from a local node.js server using the following snippet: fetch('http://localhost:3000/search/house') .then(.... Upon checking what is being sent to the server (via the network tab in Firefox dev ...

Connecting Bootstrap Tabs Dropdown to Website LinksIncorporating Bootstrap Tabs Dropdown Menu

Having an issue with my dropdown list in the Twitter Bootstrap tab - it's not responding when clicked. I've checked Stackoverflow for solutions but nothing has worked so far. I even tried removing the data-toggle='tab' attribute. Just ...

Sending dynamic data from route provider to controller

I'm currently facing an issue with passing a dynamic variable from my route to controller. The code I'm using is as follows. However, the code seems to be passing :slug instead of the actual variable string that I want. For example, when trying t ...

Tips for sending a property to a function that is imported and produces a component in React

I need help figuring out how to pass an argument back to my component library that is imported in a parent app as a dependency. The specific issue arises when I am trying to implement a brand concept in the theme object. The goal is to switch the theme bas ...

Demo showcasing the issue with the side navbar in Bootstrap not functioning as expected

I'm currently working on implementing a side nav bar using the code snippet from this link: However, when I integrate this code into my application, the left nav bar only extends to the height of the links, and the content area begins after the left ...