2017-02-17 4 views
1

Ich versuche eine Variable im Konstruktor zu setzen und benutze sie um den Pfad zum C# Controller anzugeben. Der Name der Variablen ist baseUrl.Eckige Komponente. Setze Variable im Konstruktor

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

Was gebraucht wird, ist Ihre Frage genau? – flashjpr

+0

Die Variable BaseUrl ist undefiniert, wenn ich versuche, sie zu verwenden. –

Antwort

0

Ich habe Ihren Code ein wenig formatiert und Best Practices verwendet. Dies sollte funktionieren:

(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; 
    }; 
}; 
})(); 

Kenntnis, dass sich $ rootScope nicht

Verwandte Themen