0


I'd need to use a constant to reference an URL address in my AngularJS Controller. Based on the examples I have found so far I have coded (small snippet):

var app = angular.module("customerManagement", []).constant('SERVER_URL','http://localhost:8080/customers');

app.controller("customerManagementController", function ($scope, $http) {

$scope.customers = [];
$scope.form = {
  id: -1,
  name: "",
  surname: ""
};
//Now load the data from server
_refreshPageData();
//HTTP POST/PUT methods for add/edit customers
$scope.update = function () {
  var method = "";
  var url = "";
  var data = {};
  if ($scope.form.id == -1) {
    //Id is absent so add customers - POST operation
    method = "POST";
    url = SERVER_URL;
    data.name = $scope.form.name;
    data.surname = $scope.form.surname;
  } 

But it does not seem to work. In the console I can see:

Error: SERVER_URL is not defined

What's wrong with my constant definition? Thanks

1 Answer 1

3

You need to inject it in your controller:

app.controller("customerManagementController", function ($scope, $http, 
SERVER_URL) {
...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, however I get a "SyntaxError: missing formal parameter" when I inject it into the Controller.
Got it working with: app.controller("customerManagementController", function ($scope, $http, SERVER_URL) {
Ok, I will edit again. This was my first answer before adding the quotes. Good job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.