Javascript 如何创建自定义服务?
我的控制器:Javascript 如何创建自定义服务?,javascript,angularjs,Javascript,Angularjs,我的控制器: app.controller('myCtrl', function($scope, $http, $timeout) { $scope.getData = function() { $http.get("../send") .then(function(response) { $scope.text = response.data; $scope.params = $scope.text.spl
app.controller('myCtrl', function($scope, $http, $timeout) {
$scope.getData = function() {
$http.get("../send")
.then(function(response) {
$scope.text = response.data;
$scope.params = $scope.text.split(' ');
$scope.timeFunc();
},
function(response) {
$scope.timeFunc();
});
};
$scope.timeFunc = function() {
$timeout(function() {
$scope.getData();
}, 1000);
};
$scope.getData();
});
如何做到这一点,而不是在控制器中,这是一个单独的服务?
问题是变量params
应该每秒更新一次
app.controller('myCtrl',函数($scope,$http,$timeout,servicenName){
$scope.getData=函数(){
servicenName.getData().then(函数(响应){
$scope.text=response.data;
$scope.params=$scope.text.split(“”);
$scope.timeFunc();
},
功能(响应){
$scope.timeFunc();
});
};
$scope.timeFunc=函数(){
$timeout(函数(){
$scope.getData();
}, 1000);
};
$scope.getData();
});
app.service('servicenName',函数($http){
this.getData=函数(){
返回$http.get(“../send”);
};
});代码>