Transfer the AngularJS variable's value either directly to JSP or using an intermediary JS variable

On my .jsp web page, I am facing an issue with an AngularJS nested dropdown menu:

<div ng-controller="AlgoController">
<select ng-model="myModel" ng-options="model.name group by model.shade for model in models"></select>

Although I can easily display the selected menu item's value using {{myModel.swValue1}}, I need to pass this value to my JSP code so it can determine which Java code to run.

If it were a plain JavaScript variable, I could pass it into JSP like this:

<script>
var v = "I am the selected menu item";
</script>

<br><br><br>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>

It has been suggested that I try something like:

<script>
var v = $scope.myModel;
</script>

or

<script>
var v = myModel.name;
</script>

However, these approaches seem to leave v undefined.

My latest attempt, still unsuccessful, is:

<script>
var scope = angular.element($("body")).scope();

var v = scope.myModel;
</script>

<br><br><br>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>

Inspired by a related Plunkr, I added id="mymenu" to the select element and tried accessing its value:

<script>
var v = window.document.getElementById("mymenu").selectedIndex;
document.writeln(v);
</script>

Unfortunately, this always returns -1. It seems that regular JavaScript does not communicate well with Angular, and vice versa.

Answer №1

When it comes to web development, AngularJS (a JavaScript framework) operates directly on the browser, while JSPs (JavaServer Pages) function on the server side, where they are transformed into HTML and then sent back as a response.

If you're interested in understanding where JSPs fit into the picture, take a look at this helpful diagram.

To interact with your JSP page and pass information to it, you can simply make a request to its URL.

For instance, if you wish to retrieve a list of names from a JSP named xyz.jsp, just send a request to the URL

http://<your domain>/xyz.jsp?operation=getnamelist
.

In this scenario, operation (which can be any name suitable for your needs) acts as a GET request parameter.

To sum it up, you have the ability to send specific instructions from your AngularJS code to your JSP URL, guiding it on which Java code to execute.

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

What causes my page to refresh every time I click on a blank link?

I have a link that looks like this: <a hreflang="15" class="comment-show-link" href="">View/hide comments</a> Afterwards, I use the following code to toggle the visibility of the content: $('.comment-show-link').click(function(e) { ...

What is the best way to continue invoking $broadcast when the mouse is pressed on a button in AngularJS?

I have a button on my page that looks like this: <button id="scale-up" ng-click="scaleUp()">Scale Up</button> Currently, the function "scaleUp()" is defined as follows: $scope.scaleUp = () => { $scope.$broadcast('scaleUp'); }; ...

Developing the latest version of ngx-charts

I'm currently working on adding a personalized feature to ngx-charts that I would like to include in a release version. I was successful in implementing it using the standard src directory, but now I want to build a release version for potential distr ...

Comparison of Arrays Containing Objects

I'm looking for a way to compare arrays of nested objects in Mongoose. In the scenario below, I am seeking results where the name properties match. Is there anyone who can assist me with this? Organisation.find( { $or: [ { "c ...

What are some ways I can incorporate SVG icons using the <svg> and <use> elements in my website?

I am currently working on an Angular 1.5 application that communicates with Salesforce using REST API. Salesforce requires the use of SVG icons with a specific syntax: <svg aria-hidden="true" class="slds-icon slds-icon--large slds-icon-standard-user"& ...

Encountering difficulties importing the angular datepicker feature into my application

I have been attempting to integrate the angular-ui-bootstrap datepicker into my application but I am encountering difficulty in doing so. As part of this process, I am using ES6 and below is the code structure I currently have set up: map/src/app.js imp ...

Issue: 1506741262570 Marionette ALERT Port 2828 connection established with Selenium

My goal is to utilize Selenium with Firefox to launch Google. The version of Firefox I am using is 52.3.0 (64-bit). Here is the code snippet I have tried: System.setProperty("webdriver.gecko.driver","C://geckodriver-v0.19.0-win64_2//geckodriver.exe"); ...

What could be causing the failure of role.permissions.remove()?

I attempted to create a command that would remove the MENTION_EVERYONE permission from all roles, but it seems like it's not working as expected. While I was able to identify which roles have the permission through console logging, for some reason the ...

Improprove your onprogress event frequency when uploading files with JavaScript

I am currently utilizing jQuery to facilitate the upload of a photo, and I have added an event listener for onprogress events. However, even when uploading photos that are a few megabytes in size, the onprogress event is only triggered when the upload reac ...

Encountered a problem in Node.js when attempting to generate a CSV file using Bot Framework

I am currently working on a chatbot project using the Microsoft Bot Framework with Node Js. My goal is to provide the user with a CSV file upon request. However, I have encountered an issue with the format of the downloaded file 'prova.csv' not ...

Java implementation of the classic Tic Tac Toe game

Hey everyone, I need some help with a coding issue I'm facing. I have successfully implemented the game, but now I'm stuck on coding the win conditions. I'm considering using a boolean array for each button, but I'm struggling to figure ...

Is there a way to obtain a user's screen size without relying on a physical device or measuring the diagonal?

I'm looking for a way to display something at its actual size on a browser. For instance, if something appears to be 20cm on the screen, I want it to truly measure up to 20cm. Some methods involve considering the diagonal resolution of the screen, li ...

Having trouble with JavaScript not functioning correctly within the Update Panel?

Does anyone know how to include JavaScript from a remote source inside an update panel? I'm trying to add Facebook and Twitter share buttons to my application. Here is the code snippet: The problem is that this code works fine when the page is initia ...

Deliver feedback from NodeJS Server to JavaScript on the client side

I have set up an HTTP server in NodeJS by utilizing the http.createServer(...) method. In my client-side JavaScript file, I sent a query using the POST method to the localhost URL. The server effectively received the data from the client, but now I am enco ...

Even though I've specifically instructed it not to, TinyMCE insists on removing any HTML code

I'm encountering difficulties with TinyMCE and custom tags in our custom CMS setup. Here are my current settings: tinyMCE.init({ // General options mode: "textareas", width: "200", theme: "advanced", cleanup_o ...

Fade out the notification div when the user clicks anywhere outside of it

I am currently working on a website with a notification feature. When the user clicks on the notification button, a notification box will appear at the bottom of the button. I would like the behavior of this notification to be similar to Facebook's - ...

Tips for locating the initial entry within the dom

I'm dealing with some DOM code that looks like this: ..... <ul>* <li> <ul> Sone html here </ul> </li> </ul> ..... <ul>* <li> <ul> ...

"Looking to create a voting button similar to Stackoverflow's up-down feature? Let's explore how you

Challenges to Overcome What is the best approach for creating Ajax buttons (upward and downward arrows) that allow users to increase or decrease a number? How can user actions be stored in a variable called NumberOfVotesOfQuestionID? I am undecided on w ...

.mounted middleware's redirect() function fails when using a relative destination

I need to limit access to a specific subtree only to users who have been authenticated. The setup looks like this: app.use(express.bodyParser()) .use(express.cookieParser('MY SECRET')) .use(express.cookieSession()) .use('/admin', is ...

Verify the user that is currently signed in

I am currently in the process of developing a custom middleware to facilitate user login in my application. Here is the function I have implemented for this purpose: async function authorizeUser(username, password) { try { const hashedPwd = aw ...