Trying to wire up AngularJS to a Spring MVC app (this part is largely irrelevant) and wondering what is the Angular way of passing a URL to the JS controller.
I see a lot of examples (pretty much all of them) with URLs / paths hard coded into the JS file and as a long-time MVC developer, this bothers me
function MyController($scope, $http) {
$scope.doSomething = function() {
$http.get('/someUrl').success(successCallback);
}
}
I understand I could just set a JS variable in my JSP but this doesn't feel like the right way forward.
So what is the Angular way of passing URLs from things like <spring:url> or Rails' url_for(:action => 'some_action') to Angular controllers?
I am using constants for this which can be injected as dependencies, so for instance:
angular.module('myModule', [])
.constant('urls', {
'saveUrl': '<c:url value="/save"/>'
, 'cancelUrl': '<c:url value="/cancel"/>
})
.controller('someController', function($scope, urls) {
$scope.saveUrl = urls.saveUrl;
});
however, it gets a bit harder if I want to populate some model as well. Not sure how to handle that scenario, allthough for now I have created a config section for each service, that will add the content to it by looping over the spring model. But this might be better done by letting angular perform a submit to a page and get all the data through json call. However that means that the calling page should already be able to do this.
Ok, I went with this approach though I'm still very keen to hear from others
<form name="myForm" ng-submit="submit('<spring:url value="/someUrl" />', myModel)" novalidate>
... and this in the controller
$scope.submit = function(url, model) { // ...