Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为AngularJS服务分配属性/方法_Javascript_Angularjs_Controller - Fatal编程技术网

Javascript 如何为AngularJS服务分配属性/方法

Javascript 如何为AngularJS服务分配属性/方法,javascript,angularjs,controller,Javascript,Angularjs,Controller,我创建了一个角度服务: app.service('myService', ['$http', '$q', function($http, $q){ var getInfo = function(){ return $http({ method: 'GET', url: 'X' }); }; }]); app.controller('myController', ['$scope', 'myServ

我创建了一个角度服务:

app.service('myService', ['$http', '$q', function($http, $q){
    var getInfo = function(){
        return  $http({
            method: 'GET',
            url: 'X'
        });
    };
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {


    myService.getInfo()
    .success(function(data, status, headers) {

    })
    .error(function(data, status, headers) {

    })

}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
  var myService2 = {
    foo: function() {
      console.log('foo from myService2');
    }
  };

  //what you return from a factory is the service
  return myService2;
});
而控制器无法识别它:

app.service('myService', ['$http', '$q', function($http, $q){
    var getInfo = function(){
        return  $http({
            method: 'GET',
            url: 'X'
        });
    };
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {


    myService.getInfo()
    .success(function(data, status, headers) {

    })
    .error(function(data, status, headers) {

    })

}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
  var myService2 = {
    foo: function() {
      console.log('foo from myService2');
    }
  };

  //what you return from a factory is the service
  return myService2;
});
我在控制台中收到此错误:

app.service('myService', ['$http', '$q', function($http, $q){
    var getInfo = function(){
        return  $http({
            method: 'GET',
            url: 'X'
        });
    };
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {


    myService.getInfo()
    .success(function(data, status, headers) {

    })
    .error(function(data, status, headers) {

    })

}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
  var myService2 = {
    foo: function() {
      console.log('foo from myService2');
    }
  };

  //what you return from a factory is the service
  return myService2;
});

我缺少什么?

t这应该是
这个.getInfo
,以便
getInfo
被指定为服务的一种方法

app.service('myService', ['$http', '$q', function($http, $q){
    this.getInfo = function(){
        return  $http({
            method: 'GET',
            url: 'X'
        });
    };
}]);
以下是使用Angular创建服务的两个通用示例:

app.service('myService', function() {
  // "this" is the service
  this.foo = function() {
    console.log('foo from myService1');
  };
});
使用工厂:

app.service('myService', ['$http', '$q', function($http, $q){
    var getInfo = function(){
        return  $http({
            method: 'GET',
            url: 'X'
        });
    };
}]);
app.controller('myController', ['$scope', 'myService', function ($scope, myService) {


    myService.getInfo()
    .success(function(data, status, headers) {

    })
    .error(function(data, status, headers) {

    })

}]);
TypeError: Object [object Object] has no method 'getInfo'
app.factory('myService2', function() {
  var myService2 = {
    foo: function() {
      console.log('foo from myService2');
    }
  };

  //what you return from a factory is the service
  return myService2;
});