how to get model attribute value in angularjs from

2020-02-14 04:12发布

I have 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 any way I could get attribute "Authorization" in angularjs controller?

4条回答
时光不老,我们不散
2楼-- · 2020-02-14 04:43

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

let say you have added "some-data" to the model like follows.

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

in the jsp where the ctrl is initializing add the attribute to a hidden html element such as a span

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

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

查看更多
地球回转人心会变
3楼-- · 2020-02-14 04:45

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

查看更多
再贱就再见
4楼-- · 2020-02-14 04:48

Try this:

($location.search()).Authorization

More details about $location and parameters: https://docs.angularjs.org/api/ng/service/$location

查看更多
时光不老,我们不散
5楼-- · 2020-02-14 05:00

I've faced with the same issue, but ($location.search()).attributeKey doesn't work for me. In my angularJs controller I'm receiving 'undefined' value via ($location.search()).attributeKey.

查看更多
登录 后发表回答