서비스를 사용하고 해당 생성자에서 개체를 반환하고 있습니다. 서비스는 new yourFunction
으로, 공장은 yourFunction()
으로 초기화됩니다.
서비스에서 서비스로 전환하면 올바르게 작동합니다.
EDIT : 서비스를 계속 사용하려면이 기능을 사용해보십시오. 참고 나는 서비스의 이름을 변경
function GetWidgetsService($http, globalServices){
this._$http = $http;
this._globalServices = globalServices;
}
GetWidgetsService.prototype.getData = function() {
var getWidgetUrl = this._globalServices.baseUrl + "admin/widget/list-text-widget";
// Angular $http() and then() both return promises themselves
return this._$http({method:"GET", url:getWidgetUrl}).then(function(result){
// What we return here is the data that will be accessible
// to us after the promise resolves
return result.data;
});
};
angular.module('yourModule').service('getWidgetsService', GetWidgetsService);
편집 2 : HERE이 코드는 작업으로 JSBin이다 완성도를 들어, 여기 고정 공장
angular.module('yourModule').factory('getWidgetsFactory', function ($http, globalServices) {
return {
getData: function() {
var getWidgetUrl = globalServices.baseUrl + 'admin/widget/list-text-widget';
// Angular $http() and then() both return promises themselves
return $http({method: 'GET', url: getWidgetUrl}).then(function (result) {
// What we return here is the data that will be accessible
// to us after the promise resolves
return result.data;
});
}
};
});
EDIT 3입니다 내 첫 번째 솔루션.
angular.module('dss')
.controller('widgetCtrl',
['$scope', '$compile', '$window', '$location', '$http', 'globalServices', 'getWidgets', 'Flash', '$timeout', '$sce', '$routeParams', widgetCtrl]);
function widgetCtrl($scope, $compile, $window, $location, $http, globalServices, getWidgets, Flash, $timeout, $sce, $routeParams) {
var widget = getWidgets.getData();
widget.then(
function (result) {
$scope.widgets = result; $scope.$apply();
});
}
편집 :
하면 전체 코드를 게시 할 수 있습니다 : 당신이 조언을 원하는 경우,이 구문을 사용합니까? angular.module ('...'). service (....) – hpfs
전체 코드가 추가되었습니다. 지금 확인하십시오. – Girish
Controller ('widgetCtrl', [ '$ scope', '$ compile', '$ window', '')는 컨트롤러의 정의를 게시 할 수 있습니다 (이 서비스를 사용하는 곳에서) – gianlucatursi