$http() 不是一个函数

$http() is not a function

本文关键字:一个 函数 http      更新时间:2023-09-26
 Enter Where Condition: <input type="text" id="myText">
    <button onclick="myFunction()">Go</button>
    <script type="text/javascript">
    function myFunction($http) {
    var condition = document.getElementById("myText").value;
        $http({
            method: 'POST',
            url: "http://localhost:8080/peter/peter1/where",
            params: {"where":condition},
            headers: {'Content-Type': 'application/json'}
            });
        } 
    <script/>

当我尝试调试上面的代码时,我收到一个错误,说 $http不是一个函数...在上面的代码中,我收到错误 说$http不是一个函数。请帮助我解决这个问题...

$http是使用AngularJS时可以注入的服务。

现在在代码中拥有的内容将不起作用,因为您定义了一个函数,该函数需要一个名为 $http 的参数,您尚未传递该参数。

最重要的是,它期望$http参数是一个接受对象(大概是为了将数据发布到服务器)的函数。

下面是如何在 Angular 中执行您尝试执行的操作的示例。

这是我非常喜欢 egghead.io 的角度教程

Enter Where Condition: <input type="text" id="myText">
<button onclick="myFunction($my_http)">Go</button>
<script type="text/javascript">
function myFunction($http) {
var condition = document.getElementById("myText").value;
    $http({
        method: 'POST',
        url: "http://localhost:8080/peter/peter1/where",
        params: {"where":condition},
        headers: {'Content-Type': 'application/json'}
        });
    } 
<script/>

请注意,当您调用函数 myFunction() 时,应该使用包含 $http 对象的参数调用它,如下所示:

myFunction($http_example);

而不是这样:

myFunction();