充分利用Spring MVC的数据角JS在初始查看呼叫 [英] Getting data from Spring MVC in Angular JS in the initial view call

查看:177
本文介绍了充分利用Spring MVC的数据角JS在初始查看呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角JS,我已创建了角的JS Spring MVC的Web应用程序,我知道,从视图,我们可以利用的资源,restangular,HTTP调用从角JS REST服务,但在春说形成控制器一观点被触发并认为再从角REST调用是从以服务器称为内通过角加载DATAS,此后得到DATAS加载,而不是有没有办法通过JSON对象而引发的看法从弹簧控制器到角JS在第一时间本身。

我也做了类似的事情,它工作正常,但不知道是不是它的一个很好的方法还是不行。

春季控制器

  @RequestMapping(/ getEmployee的)
公众的ModelAndView HELLOWORD(){
   JSONArray employeeJsonArray = //包含了员工的全部信息
   返回新的ModelAndView(雇员,雇员,employeeJsonArray);
}

employee.jsp

 < HTML NG-应用=对myApp>
< HEAD>
< META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= ISO-8859-1>
<标题> Spring 3.0的MVC系列信息:Hello World< /标题>
&所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js>&下; /脚本>
<脚本>
VAR应用= angular.module('对myApp',[]);
app.controller('MyCtrl',函数($范围){
    $ scope.employee = [];
    $ scope.loadData =功能(职员)
    {
        $ scope.employee = JSON.parse(员工);
    };
});
< / SCRIPT>
< /头>
<机身NG控制器=MyCtrl>
{{loadData('$ {}员工')}} <输入类型=文本NG值=员工[0]。名称/>
< /身体GT;
< / HTML>


解决方案

角真正的亮点,当你在一个单一的页面应用程序中使用它,所以我会建议使用它,你在你的现行办法建议作为其移动你更接近一个单页的应用程序设计的,但是,如果你只是想获得一些的角度特性在现有的请求/响应的应用程​​序,那么你可以发送与数据的HTML有效载荷在纳克-init ,因为这页面演示。这是一个回报率的例子,但我认为这一点是明确的。我认为这是一个有点黑客,而是应该把工作做好。

 <机身NG-的init =angularVariable = $ {} variableFromServer>
     HTML /角...
   < /身体GT;

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.

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

<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>

解决方案

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 page demonstrates. 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.

   <body ng-init="angularVariable = ${variableFromServer}">
     html/angular ...
   </body>

这篇关于充分利用Spring MVC的数据角JS在初始查看呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆