How to get model attribute value in angularjs from spring controller

I have a spring controller defined as

@Controller
public class TestController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value="/hello")
    public String home(HttpServletRequest httpRequest, Model model) {        

            model.addAttribute("Authorization", "test string");

            return "/index";
    }  
}

Is there a way to get the "Authorization" attribute in an angularjs controller?

+4
source share
4 answers

Try the following:

($location.search()).Authorization

More about $locationand options: https://docs.angularjs.org/api/ng/service/ $ location

+1
source

I ran into the same problem but ($location.search()).attributeKeynot working for me. In my angularJs controller, I get the value undefined via ($location.search()).attributeKey.

0
source

try ng-init = "var = $ {Authorization}" then in the controller $ scope.var

0
source

I have had this problem for some time. I used the "hacker" way to access the variable added as a modal attribute in the backend.

let's say you added "some-data"to the model as shown below.

model.addAttribute("some-data-id", "some-data");

in jsp where ctrl initializes adding an attribute to a hidden html element such as span

<span style="display:none" id="someDataId">${some-data-id}</span>

and in the controller, extract the value with angular.element('#some-data-id').innerHTML

0
source

All Articles