I'm trying to develop a software tool where people can enter an ID and then that ID will be searched using a third party website in the background and the results of that search will be displayed in a different fashion on my software. However, my front-end is in AngularJS while the back-end code is all in Java. How do I get Angular to pass that data to a function or similar? My coworker said something about passing the value to a controller and having that interact with the java program, but I didn't really understand what he was trying to say.
But for example, to make my question more clear:
Say I have this HTML/Angular code:
<div>
Name:
<br />
<input type="text" ng-model="name"> {{ name }} </input>
<br />
</div>
<script src="angular.min.js"></script>
and then I have this Java code:
public int letterCount(String name){
return name.length();
}
How do I pass the variable "name" from the first file to the java method "letterCount" ??
I'm trying to develop a software tool where people can enter an ID and then that ID will be searched using a third party website in the background and the results of that search will be displayed in a different fashion on my software. However, my front-end is in AngularJS while the back-end code is all in Java. How do I get Angular to pass that data to a function or similar? My coworker said something about passing the value to a controller and having that interact with the java program, but I didn't really understand what he was trying to say.
But for example, to make my question more clear:
Say I have this HTML/Angular code:
<div>
Name:
<br />
<input type="text" ng-model="name"> {{ name }} </input>
<br />
</div>
<script src="angular.min.js"></script>
and then I have this Java code:
public int letterCount(String name){
return name.length();
}
How do I pass the variable "name" from the first file to the java method "letterCount" ??
Share Improve this question asked Jun 8, 2016 at 20:17 Anon YmousAnon Ymous 311 silver badge4 bronze badges2 Answers
Reset to default 3What do you mean by 'while the back-end code is all in Java.'?
is it in the same project? (or) is it part of some rest services?
If your back-end code is developed with REST end points (URLs), then use them in Angular JS to post and get the data. Rest example.
if your back-end java code is part of the same project (MVC), then usually you will have a servlet (controller) URL to which you can send the data to invoke the back end logic. Servlet example.
Controller thing is javascript, i.e. ".js" file. which take care of things happening in html page. Call java method in controller.js and pass the variable 'name' to it, better use one service method also.
$scope.methodFromHtmlPage = function(name){
UrControllerToHandleThisModule.methodInSvc(name).then(
function(response){
// u have response data here
},
function(error){
// do something
}
Now in service.js
function methodInSvc(name)
return $http.post('http:// url to java method which will handle this req', name).then(
function (response) {
//do things on success
},
function (error) {
//do things on error
});
Use @RequestBody in java method...to get name from UI side.
public int letterCount(@RequestBody String name){
return name.length();
}