简体   繁体   English

如何在Angular和Java之间进行交互(来回传递变量)?

[英]How to interact between Angular and Java (in terms of passing variables back and forth)?

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. 我正在尝试开发一个软件工具,人们可以在其中输入ID,然后在后台使用第三方网站搜索该ID,搜索结果将以不同的方式显示在我的软件上。 However, my front-end is in AngularJS while the back-end code is all in Java. 但是,我的前端是AngularJS,而后端代码全部是Java。 How do I get Angular to pass that data to a function or similar? 如何让Angular将该数据传递给函数或类似函数? 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. 我的同事说了一些关于将值传递给控制器​​并让它与java程序交互的东西,但我并不真正理解他想说的是什么。

But for example, to make my question more clear: 但是,例如,让我的问题更清楚:

Say I have this HTML/Angular code: 说我有这个HTML / Angular代码:

<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: 然后我有这个Java代码:

public int letterCount(String name){
    return name.length();
}

How do I pass the variable "name" from the first file to the java method "letterCount" ?? 如何将变量“name”从第一个文件传递给java方法“letterCount”?

What do you mean by 'while the back-end code is all in Java.'? 你的意思是'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端点(URL)开发的,那么在Angular JS中使用它们来发布和获取数据。 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. 如果你的后端java代码是同一个项目(MVC)的一部分,那么通常你会有一个servlet(控制器)URL,你可以发送数据来调用后端逻辑。 Servlet example. Servlet示例。

Controller thing is javascript, ie ".js" file. 控制器的东西是javascript,即“.js”文件。 which take care of things happening in html page. 它处理html页面中发生的事情。 Call java method in controller.js and pass the variable 'name' to it, better use one service method also. 在controller.js中调用java方法并将变量'name'传递给它,最好也使用一个服务方法。

$scope.methodFromHtmlPage = function(name){
UrControllerToHandleThisModule.methodInSvc(name).then(
 function(response){
   // u have response data here
 },
 function(error){
   // do something
 }

Now in service.js 现在在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. 在java方法中使用@RequestBody ...从UI端获取名称。

public int letterCount(@RequestBody String name){
  return name.length();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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