简体   繁体   中英

how to pass value in angular js in jsp/servlet

I am developing a module in jsp/servlet in which i need to use angular js. how could i pass the value from input box value in function then how to send value in servlet also...!

Here the example of code:-

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
  <!DOCTYPE html>
  <html ng-app="mehandi" >
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>


      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js">  
     </script>
     <script>
        var app = angular.module('mehandi', []);
        app.controller('mhndiCtrl', function($http){
            var xyz = this;
            xyz.data = {}
            xyz.swati = function($http){


            }

        })


    </script>
  </head>
  <body ng-controller="mhndiCtrl as abc" >
    <h1>Hello World!</h1>
    <input type="text" ng-model="abc.data.myname">
    {{abc.data}}
    <button ng-click="abc.swati()" > </button>
   </body>
</html>

You can invoke the function via ng-init : in your html

ng-init="init('${userDTO.username}')"

So, in your *.js ng-controller it will be following:

$scope.init = function(username){
   /*logic code*/
};

Use the name attribute of input for servlets

<input type="text" name="firstname">

You can then retrieve it using

request.getParameter("firstname")

The angular part remains the same

<input type="text" name="firstname" ng-model="abc.data.myname">

Remember, angularjs is a client side javascript framework, hence server side bindings remains the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM