Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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
js之外的Javascript_Javascript_Angularjs - Fatal编程技术网

js之外的Javascript

js之外的Javascript,javascript,angularjs,Javascript,Angularjs,在Angular JS中,我可以在控制器外部创建Javascript函数,并在需要时调用它,而不是在模块中使用服务吗 例如: var UrlPath="http://www.w3schools.com//angular//customers.php" //this will contain all your functions function testing_Model ($http){ var self=this; self.getRecords=function(){ $http

在Angular JS中,我可以在控制器外部创建Javascript函数,并在需要时调用它,而不是在模块中使用服务吗

例如:

var UrlPath="http://www.w3schools.com//angular//customers.php"
//this will contain all your functions
 function testing_Model ($http){
 var self=this;

 self.getRecords=function(){
 $http({
  Method:'GET',
  URL: UrlPath,
 }).success(function(data){
  self.record=data.records;
});

}
self.getRecords();
}

angular.module("myApp", []);
app.controller('myController', function ($scope, $http) {
$scope.testing_Model = new testing_Model();}
当我运行上面的代码时,它说“$http不是一个函数”


提前感谢。

您需要将$http传递给此函数

$scope.testing_Model = new testing_Model($http);

虽然它会起作用,但它显然违背了角度的概念。您必须将函数作为服务注入控制器中,以编写可测试代码,跟踪依赖项,并尊重稍后将支持您的项目的其他开发人员。

是的,他们肯定会,但请注意,服务是单例的,同时您的函数是类。您可以将此功能设置为服务的一部分,例如,您需要像调用新myService一样调用它。测试\u Model()。好的方面是,
$http
服务将被注入到您的服务中,只要您的控制器中不需要它(某种封装)。