我试图在构造函数中设置一个变量,并用它来指定c#controller的路径。变量的名称是baseUrl。
angular.module('adminService', []).factory('adminService', function ($rootScope, $http, $location) {
var adminService = function () {
this.baseUrl = $location.protocol() + "://" + location.host + "/";
}
adminService.prototype.getTopics = function () {
var promise = $http(
{
method: 'POST',
url: baseUrl + '/Admin/getTopics',
contentType: 'application/json'
}).catch(function (e) {
console.log(e);
});
return promise;
}
return new adminService;
});
答案 0 :(得分:0)
我已经编写了一些代码并使用了最佳实践。这应该有效:
(function() {
'use strict';
angular.module('adminService', []) // be CAREFUL bcz you are CREATING the module here!
.factory('adminService', adminService);
function adminService($rootScope, $http, $location) {
return {
getTopics: getTopics
};
this.baseUrl = $location.protocol() + "://" + location.host + "/";
var vm = this;
var getTopics = function () {
var promise = $http(
{
method: 'POST',
url: vm.baseUrl + '/Admin/getTopics',
contentType: 'application/json'
}).catch(function (e) {
console.log(e);
});
return promise;
};
};
})();
请注意$ rootScope未使用