Angular.js调用$ http.get从外部控制器 [英] Angular.js Call $http.get from outside controller
本文介绍了Angular.js调用$ http.get从外部控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从数据库返回前10个实体的JSON列表中的HTTP资源。
我把这种方式:
I have an HTTP resource that returns a JSON list of top 10 entities from a database. I call it this way:
var filter= "john";
var myApp = angular.module('myApp', []);
myApp.controller('BuscaController', ['$scope','$http', function ($scope, $http) {
$http.get('/api/Entity/Find/' + filter). //Get entities filtered
success(function (data, status, headers, config) {
$scope.entidades = data;
}).
error(function () {
});
}]);
它的工作原理!
但是...我怎么能修改过滤器
变量,以更改查询?
我应该重写整个控制器得到这个工作?
But... how can I change the filter
variable in order to change the query?
Should I rewrite the whole controller to get this to work?
推荐答案
关于你的问题...调用$ http.get从外部控制器,您可以做到以下几点:
Regarding to your question "... call $http.get from outside controller" you can do the following:
... ANYWHERE IN YOUR CODE
var $http = angular.element('html').injector().get('$http');
$http.get(...).success(...);
ANYWHERE IN YOUR CODE ...
请参阅从官方的角度文档:角$注射器文档:
在 GET(名称);
方法返回服务的实例
See official docs from angular: angular $injector docs :
The get(name);
method Returns an instance of the service.
这篇关于Angular.js调用$ http.get从外部控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文