简体   繁体   English

ng-init无法正常工作angularjs

[英]ng-init not working angularjs

I am getting parse error 我收到解析错误

Syntax Error: Token '{' is an unexpected token at column 8 of the expression [user= ${user}] starting at [{user}]. 语法错误:令牌'{'是表达式[user = $ {user}]的第8列中从[{user}]开始的意外令牌。

home.html 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 . 引用此示例,我将模型发送到angularjs 在初始视图调用中从Angular JS中的Spring MVC获取数据

controller.js 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 谢谢

Try changing your controller, so instead of sending just a String with the view name, you send a ModelAndView object: 尝试更改您的控制器,因此发送一个ModelAndView对象,而不是仅发送带有视图名称的String:

@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: 如果您使用百里香叶,则需要在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. 过去,我发现自己遇到了这个问题。

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. 认为您是春季代码, $user填充工作正常。

I had used resolve from angularjs to populate with user details. 我曾使用angularjs的resolve来填充用户详细信息。

$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 在这里allConstants是整个应用程序控制器都可以访问的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM