我在工厂里创建了一些功能
App.factory('CustomHttpRequest', function ($http, $q) {
return {
ajaxRequest: function () {
return "test";
}
}
});
我希望在服务中访问它,如下所示
hhwtApp.service('listPlacesService', ['$rootScope', 'CustomHttpRequest', function($rootScope, CustomHttpRequest) {
this.listPlacesData = function () {
CustomHttpRequest.ajaxRequest();
}
}]);
答案 0 :(得分:1)
是的,你可以使用如下,你几乎接近......
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope,listPlacesService) {
$scope.name = 'World';
$scope.checking =listPlacesService.listPlacesData();
console.log($scope.checking);
});
app.service('listPlacesService', [ 'CustomHttpRequest', function( CustomHttpRequest) {
this.listPlacesData = function () {
return CustomHttpRequest.ajaxRequest();
}
}]);
app.factory('CustomHttpRequest', function () {
return {
ajaxRequest: function () {
return "test";
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{checking}} done!</p>
</body>
</html>
检查将“test”返回控制器的控制台
答案 1 :(得分:0)
你已经注射了它。确保模块已连接。