0

How do i bind a value that is received in the .jsp from a java class(using model map add attribute method) to ng-model in angular js? I have tried the below options but the value is not being fetched into the model.

  1) <input ng-show=true name="emailId" id="check" type="text"
                ng-model= "user.name" ng-init="user.name=${UserName}">
  2)<input ng-show=true name="emailId" id="check" type="text"
                ng-model= "user.name" ng-init="user.name={{UserName}}">
Batman
  • 17
  • 8
  • how about `value=${UserName}`? assuming `UserName` is a variable you added in ModelMap in your Java Controller.. – Jos Apr 24 '16 at 08:38
  • Thank you so much redflar3. it works fine now. – Batman Apr 24 '16 at 10:43
  • I basically wanted to use the two way binding capability of the ng-model to fetch the value that is obtained from the java class into the controller,put it in rootscope so that the value is accessible in all the controllers. I think this is not an efficient way of doing it.Is there any other method that can be used to pass the value from the jsp to make it available for multiple controllers? – Batman Apr 24 '16 at 11:03

1 Answers1

0

I don't see value attribute there at your input element.

<input ng-show=true name="emailId" id="check" type="text" value = ${UserName}
                ng-model= "user.name" ng-init="user.name=${UserName}">

As said in comments either map your value to user.name or ${UserName}. Since in your ng-init you are mapping user.name modlel to your jQuery ${UserName}.

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • I basically wanted to use the two way binding capability of the ng-model to fetch the value that is obtained from the java class into the controller,put it in rootscope so that the value is accessible in all the controllers. I think this is not an efficient way of doing it.Is there any other method that can be used to pass the value from the jsp to make it available for multiple controllers? – Batman Apr 24 '16 at 11:00
  • @Batman: You can put that into a service and make it available in all controllers... that's another approach which is not costly. Look at my answer given here... http://stackoverflow.com/questions/22584342/how-do-i-access-the-scope-variable-of-one-controller-from-another-in-angular-js/22584430#22584430 – Thalaivar Apr 24 '16 at 21:30
  • 1
    Thank you so much for your help.I am new to angular and this really helped me. – Batman Apr 25 '16 at 03:32