在阅读了几篇关于避免范围汤的 articles 并引用 Google Guidelines 来构建 Controller 后,我留下了一个棘手的问题。我应该如何引用我的注入(inject)
我的 Controller 中的依赖项?
到目前为止,我的方法是将服务放在我的对象上,但我对此并不完全满意,因为现在我的服务暴露于外部世界(模板标记)。
在不直接引用 $scope 的情况下构建 Controller 的正确方法是什么,并使我注入(inject)的依赖项可用于 Controller 但未公开公开。
正如您在下面看到的,我的解决方法是将 $http 放在“this”上,然后在我的原型(prototype)中引用它
职能。由于上述原因,这不是我的理想选择。
http://plnkr.co/edit/Tn6Jkk06Zdu92uTM0UdU?p=preview
DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.3.0-rc2" src="https://code.angularjs.org/1.3.0-rc.2/angular.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body ng-controller="textController as txtCtrl">
<h1>{{txtCtrl.greeting}}</h1>
<button ng-click="txtCtrl.getData()">Get Names</button>
<hr>
{{txtCtrl.names | json}}
<script>
var textController = function($http){
this.greeting="Hello World";
this.$http = $http;
}
textController.prototype.alert = function(){
alert(this.greeting);
}
/*retrieve test data from //filltext.com*/
textController.prototype.getData = function(){
var that = this;
this.$http({
method: 'GET',
url: 'http://www.filltext.com/?rows=10&fname={firstName}&lname={lastName}'
})
.success(function(data){
that.names=data;
})
.error();
}
angular.module("app",[])
.controller("textController",textController);
angular.bootstrap(document,["app"]);
</script>
最佳答案
您可以做的一种方法是使用 IIFE 创建一个闭包变量并将 http service
的引用保留在 Controller 之外,并在 Controller 中使用它。
(function(){
var _$http; //<-- Here a private variable
......
/*retrieve test data from //filltext.com*/
textController.prototype.getData = function(){
var that = this;
_$http({ //<-- Use it
method: 'GET',
url: 'http://www.filltext.com/?rows=10&fname={firstName}&lname={lastName}' })...
//...
}
angular.module("app").controller("textController",textController);
})();
Plnkr
或者将属性添加到构造函数而不是其实例。
(function(){
var textController = function($http){
this.greeting="Hello World";
textController._$http = $http; //<-- Here set it
}
//....
/*retrieve test data from //filltext.com*/
textController.prototype.getData = function(){
var that = this;
textController._$http({ //<-- Use it
method: 'GET',
url: 'http://www.filltext.com/?rows=10&fname={firstName}&lname={lastName}'})...
//...
}
//...
angular.module("app").controller("textController",textController);
})();
Plnkr
这是您可以显式注释依赖项的方法(除非您使用 ng-annotate 它将在缩小时使用它)
angular.module("app").controller("textController",['$http', 'blah', textController]);
或者
textController.$inject = ['$http', 'blah'];
但是,我只负责进行 ajax 调用和任何数据映射到服务:-
例子:-
angular.module("app").service('UserService', ['$http', function($http){
this.getUsers = function(searchObj){ //Get the input and set your url
return $http({
method: 'GET',
url: 'http://www.filltext.com/?rows=10',
params: searchObj //<-- Pass params
});
}
}]);
只需在 Controller 中注入(inject) userService 即可。
var textController = function(userSvc){
this.greeting="Hello World";
this.userSvc = userSvc;
}
....
/*retrieve test data from //filltext.com*/
textController.prototype.getData = function(){
var that = this;
//Call service with argument
this.userSvc.getUsers({firstName:$scope.fn, lastName:$scope.ln}).success(function(data){
that.names=data;
})...;
}
....
angular.module("app").controller("textController",['UserService', textController]);
Plnk3
关于javascript - 我应该如何在不使用范围的情况下在我的 Controller 函数中引用服务?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26260444/