javascript 在初始视图调用中从 Angular JS 中的 Spring MVC 获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24106610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting data from Spring MVC in Angular JS in the initial view call
提问by Alex Man
I am new to Angular JS, I have created a Spring MVC web application with Angular JS, I know that from view we can call REST services from Angular JS using resource, restangular, http , But say in Spring form the Controller a view is been triggered and for loading the datas through angular within the view again a REST call from angular is been called from view to the server and gets the datas thereafter for loading, Instead is there any way to pass the json object while triggering the view from Spring controller to the Angular JS at the first time itself.
我是 Angular JS 的新手,我已经用 Angular JS 创建了一个 Spring MVC Web 应用程序,我知道从视图中我们可以使用资源、restangular、http 从 Angular JS 调用 REST 服务,但是在 Spring 表单中说控制器视图是触发并再次通过视图中的 angular 加载数据,从 angular 调用 REST 调用从视图到服务器,然后获取数据以进行加载,而不是有任何方法在从 Spring 控制器触发视图的同时传递 json 对象第一次访问 Angular JS。
I have done a similar thing, its working fine but don't know whether its a good approach or not.
我做过类似的事情,它工作正常,但不知道它是否是一个好方法。
Spring controller
弹簧控制器
@RequestMapping("/getemployee")
public ModelAndView helloWord(){
JSONArray employeeJsonArray = // contains all the information of the employee
return new ModelAndView("employee", "employee",employeeJsonArray);
}
employee.jsp
员工.jsp
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.0 MVC Series: Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('MyCtrl', function($scope) {
$scope.employee = [];
$scope.loadData = function(employee)
{
$scope.employee = JSON.parse(employee);
};
});
</script>
</head>
<body ng-controller="MyCtrl">
{{loadData('${employee}')}}
<input type="text" ng-value="employee[0].name"/>
</body>
</html>
采纳答案by dspies
Angular really shines when you use it in a "single" page application, so I would suggest using it as you suggested in your "existing approach" as it moves you closer to a single page app design, however, if you are just trying to get some of the features of Angular in an existing request/response application, then you could send your html payload with the data in ng-init
as this pagedemonstrates. It's a RoR example, but I think the point is clear. I think it is a bit of hack, but should get the job done.
当您在“单”页面应用程序中使用 Angular 时,它确实大放异彩,因此我建议您按照“现有方法”中的建议使用它,因为它使您更接近单页面应用程序设计,但是,如果您只是想在现有的请求/响应应用程序中获取 Angular 的一些功能,然后您可以发送带有数据的 html 负载,ng-init
如本页所示。这是一个 RoR 示例,但我认为这一点很清楚。我认为这有点黑客,但应该完成工作。
<body ng-init="angularVariable = ${variableFromServer}">
html/angular ...
</body>
回答by NimChimpsky
I recently switched to using angular, the beauty of it is you can ditch jsp's entirely and just have static html as the frontend. Served as a static resource by spring.
我最近转而使用 angular,它的美妙之处在于您可以完全放弃 jsp,而只需将静态 html 作为前端。由 spring 用作静态资源。
To initially populate your form/tables/whatever - lots of different options do it in javascript/angular. But its nice to keep your view seperate from your controllers (ie don't use jsp's).
最初填充您的表单/表格/任何东西 - 在 javascript/angular 中有很多不同的选项。但是将视图与控制器分开是很好的(即不要使用jsp)。
回答by Darshaa
You can use below code for get user logging,
您可以使用以下代码获取用户登录信息,
<input type="text" ng-model ="currentLoging" ng-init= "currentLoging='${pageContext.request.userPrincipal.name}'" />