0

I am getting parse error

Syntax Error: Token '{' is an unexpected token at column 8 of the expression [user= ${user}] starting at [{user}].

home.html

 <body ng-controller="mainCtrl" ng-init="user= ${user}">

Referring this example ,I am sending model to angularjs Getting data from Spring MVC in Angular JS in the initial view call.

controller.js

angular.module('userSystem', [ 'ngRoute' ]).config(

  function($routeProvider, $httpProvider) {

    $routeProvider.when('/', {
      templateUrl : 'home.html',
      controller : 'home'
    }).when('/login', {
      templateUrl : 'login.html',
      controller : 'navigation'
    }).otherwise('/');

}).controller('mainCtrl',
    function($rootScope, $scope, $http, $location, $route) {
})
});

Spring Controller

@RequestMapping(value = "/", method = RequestMethod.GET)
   public  String getIndex(Model model)
   {
     model.addAttribute("user","user");
     return "home";

   }

Please let me know what is wrong here and how to fix it. Thanks

4
  • Can you post the code for mainCtrl as well? Commented May 9, 2016 at 13:12
  • Thanks for updating. It appears you're declaring your controller incorrectly, it should look something like this .controller('mainCtrl', ["$rootScope", "$scope", "$http", "$location", "$route", function($rootScope, $scope, $http, $location, $route) {}]); <- here I'm passing the names of the function dependencies in the array as preceeding elements. Commented May 9, 2016 at 13:30
  • user I am returning from spring,I have added the code Commented May 9, 2016 at 13:34
  • Can you post the code you're using for that? Commented May 9, 2016 at 13:35

3 Answers 3

1

Try changing your controller, so instead of sending just a String with the view name, you send a ModelAndView object:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView getIndex() {

    ModelAndView mv = new ModelAndView("home");
    mv.addObject("user", "USER_NAME");
    return mv;
}

On the other hand, are you using any templating framework (thymeleaf, velocity...)? Cause in that case, your problem may be in how you pick up the model attribute in front-end.

EDIT (as an answer to the templating framework question):

In case you're using thymeleaf, you would need to do something like this in your index.hmtl:

<body ng-controller="mainCtrl as main" data-th-attr="ng-init='main.user=\''+${user}+'\''>

I found myself with this problem in the past.

Sign up to request clarification or add additional context in comments.

1 Comment

Also as a good practice, notice that I setted an alias to the controller to aboid unwanted scope problems in your front-end app. Also it's more clear what is what
0

If you remove the curley braces around user and change to this it will remove your syntax error for you:

<body ng-controller="mainCtrl" ng-init="user= $user">

Taken that you're spring code and population of $user is working correctly.

4 Comments

On removing braces,user value is undefined
You'll need to add $user as a dependency to mainCtrl
I cannot add dependency as $user, as user is neither service nor controller
I'm not familiar with spring so I'm not sure what it's doing, it looks like you're setting the user attribute on the model to the string "user". Is this supposed to be a string? Also, how are you accessing the user attribute on the model from JavaScript?
0

I had used resolve from angularjs to populate with user details.

$routeProvider.when('/', {
      templateUrl : 'home.html',
      controller : 'home',
      resolve: {
        message: function(messageService){
            var promise= messageService.getMessage();
            promise.then(data){
            allConstants.user=data;
            }
            return promise;
        }
    }
    }).when('/login', {
      templateUrl : 'login.html',
      controller : 'navigation'
    }).otherwise('/');

Here allConstants is a varible accessible by entire application controllers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.