javascript - Getting data from Spring MVC in Angular JS in the initial view call -
i new angular js, have created spring mvc web application angular js, know view can call rest services angular js using resource, restangular, http , in spring form controller view been triggered , loading datas through angular within view again rest call angular been called view server , gets datas thereafter loading, instead there way pass json object while triggering view spring controller angular js @ first time itself.
i have done similar thing, working fine don't know whether approach or not.
spring controller
@requestmapping("/getemployee") public modelandview helloword(){ jsonarray employeejsonarray = // contains information of 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 shines when use in "single" page application, suggest using suggested in "existing approach" moves closer single page app design, however, if trying of features of angular in existing request/response application, send html payload data in ng-init
page demonstrates. it's ror example, think point clear. think bit of hack, should job done.
<body ng-init="angularvariable = ${variablefromserver}"> html/angular ... </body>
Comments
Post a Comment