如何在 Angular js 中获取 $http 服务,然后在我的自定义 asyncAction 函数调用中对其调用 get 函数。不涉及 html( Bootstrap ???)
编辑:可以放置一些虚拟 html 使其工作以触发 Controller 某些东西
我在 http://jsfiddle.net/smartdev101/bdmkvr6g/ 有一个 jsfiddle
asyncAction: function(resultFunction, faultFunction) {
$http.get("https://api.github.com/users/angular")
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log(data);
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
},
一些进展 - 更新了 fiddle,添加了一些 html 标签以使其工作,但是
有人可以找到我的方法,而无需在我的代码中插入标记,甚至可以在没有此 ng-controller
标记的情况下自己触发 AttendeeProxyController
吗????我更喜欢完全手动启动和 Controller 触发过程。
或者横向思考...我根本不需要这个 Controller ,有了它我就可以在没有 html 的 javascript 中访问 $http?
<span id="attendeeProxyController" ng-controller="AttndeeProxyController"></span>
最佳答案
OR lateral thinking... I don't event need this controller, it's there so that I can get access of $http purely just in javascript without html?
是的,虽然我不建议这样做,但还是可以的
var $http = angular.injector(["ng"]).get("$http");
// use $http here
这将为您提供对 $http 的直接引用,您可以使用它。然而,它与依赖注入(inject)的 Angular 方式背道而驰,如果你不小心,它通常会更难测试和推理代码。
关于javascript - 获取 $http 服务并调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26726010/