user3802040 February 2016

Spring dont see angular

I am trying adapt my app to use angular, but currently it seems that spring cannot see/communicate with angular. Here is what i have :

Controller.java:

@Controller
public class IndexController {
 @RequestMapping(value = "/login", method = RequestMethod.GET, produces = {"application/json"})
 public @ResponseBody Map<String, Object> getIndexPage() {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("id", UUID.randomUUID().toString());
    model.put("content", "Hello Worl123321");
    return model;
 } 
}

index.html:

<!doctype html>
<html>

  <head>
      <title>Title</title>
      <!--<link href="css/angular-bootstrap.css" rel="stylesheet">
      <link rel="stylesheet" href="css/app.css"/>-->
      <style type="text/css">
        [ng\:cloak],
        [ng-cloak],
        .ng-cloak {
            display: none !important;
        }
      </style>
      <script src="js/angular-bootstrap.js" type="text/javascript"></script>>
      <script src="app/app.js"></script>
      <script src="app/listView/listView.js"></script>
  </head>

  <body ng-app="myApp">
      <!-- Application content -->
      <div ng-include="'app/app.html'"></div>
  </body>

</html>

app.html:

<div class="container">
    <div ng-controller="appCtrl" ng-cloak class="ng-cloak">
        {{greeting.content}}
    </div>
</div>

app.js:

angular.module('myApp', [])
    .controller('appCtrl', function($scope, $http) {
        $http.get('/login/').success(function(data) {
            $scope.greeting = data;
        });
});

I just get an output like this :

{"id":"01cdab29-e0ce-45ee-abb9-64b2640859ca","content":"Hello Worl123321"}

Answers


Walfrat February 2016

What do you use to serialise as JSON ?

This is maybe because of the Map<> that you have an extra field 'content' between your object and your data try.

 $scope.greeting = data.content;

I've already heard of some problem like this with Jackson adding a "content" though it was for a hibernate problem.

If you don't want that extra field : create a wrapper class having an id and content field and use it.

Post Status

Asked in February 2016
Viewed 2,484 times
Voted 7
Answered 1 times

Search




Leave an answer