我需要在AngularJS控制器中使用常量引用URL地址。根据我到目前为止发现的示例,我已经进行了编码(小片段):
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;
}
但是它似乎不起作用。在控制台中,我可以看到:
Error: SERVER_URL is not defined
我的常量定义怎么了? 谢谢
答案 0 :(得分:3)
您需要将其注入您的控制器中
app.controller("customerManagementController", function ($scope, $http,
SERVER_URL) {
...
}