javascript - 获取 $http 服务并调用

标签 javascript angularjs

如何在 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/

相关文章:

javascript检查链接内的文本 anchor 文本是否溢出

javascript - 我如何使用 JavaScript 将结果与其他文本一起使用

javascript - 动态创建的文本输入不会调整宽度

javascript - 在不刷新的情况下监听事件和更新页面

javascript - 周围最近的坐标

javascript - JQuery-ui,自动隐藏对话框

javascript - Angular : Update service and share data between controllers

javascript - 如何在 angularjs ui-router 中的状态之间共享 $scope 数据?

javascript - Karma + Webpack 测试意外 token '/'

javascript - 使用 AngularJS 清理我的 View