$ httpBackend .whenGet不是Angular中的函数

时间:2015-10-04 12:56:18

标签: angularjs ngmock

我正在尝试使用虚拟服务来获取Angular中的数据,我正在使用ngMockE2E,而我的Mock代码看起来像这样:

(function () {
    "use strict"
    var app = angular
        .module("productResourceMock", ["ngMockE2E"]);

    app.run(function ($httpBackend) {
       var products = [
           {
               "productId": 1,
               "productName": "mobile1",
               "productCode": "heh4",
               "releaseDate": "May 21, 2013",
               "description": "very nice mobile",
               "cost": 200,
               "price": 300,
               "category": "Electronics",
               "tags": ["mobile", "electronic"],
               "imageUrl": "images/img1.jpg"
           },
           {
               "productId": 2,
               "productName": "mobile2",
               "productCode": "heh4",
               "releaseDate": "May 21, 2012",
               "description": "not a nice mobile",
               "cost": 100,
               "price": 500,
               "category": "Electronics",
               "tags": ["mobile", "Electronic"],
               "imageUrl": "images/img2.jpg"
           }];

        var productUrl = "/api/products";
        $httpBackend.whenGet(productUrl).respond(products);
    });

}());

我已经定义了我的控制器,在其中,它有这个代码:

(function () {
    "use strict"
    angular
        .module("productManagement")
        .controller("ProductListCtrl",
                    ["productResource",
                        ProductListCtrl]);

    function ProductListCtrl(productResource) {
        var vm = this;
        productResource.query(function(data){
         vm.products = data;
        });
    }
}());

对于发送REST请求的服务,我有以下代码:

(function () {
    "use strict"
    angular
        .module("common.services")
        .factory("productResource",
                ["$resource", productResource]);


    function productResource($resource) {
        return $resource("/api/products/:productId");
    }
}());

我仍然收到此错误:未捕获TypeError:$ httpBackend.whenGet不是函数。

如有任何帮助,或需要任何澄清,请告知我们。

1 个答案:

答案 0 :(得分:5)

答案很简单:将<section> <h2>Write us a message</h2> <div class="contact"> <?php if(!empty($errors)): ?> <div class="panel"> <ul><li> <?php echo implode('</li><li> ',$errors); ?> </li></ul> </div> <?php endif; ?> <form method="post" action="contact-script.php"> <label> <img src="img/person.png" alt="Person Icon"> Nome* <input type="text" name="name" autocomplete="off" <?php echo isset($fields['name']) ? 'value="' . e($fields['name']) . '"' : '' ?> /> </label> <label> <img src="img/telephone.png" alt="Telephone Icon"> Telefono <input type="text" name="tel" autocomplete="off" <?php echo isset($sec_fields['tel']) ? 'value="' . e($sec_fields['tel']) . '"' : '' ?> /> </label> <label> <img src="img/mail.png" alt="Mail Icon">Indirizzo email* <input type="text" name="email" autocomplete="off" <?php echo isset($fields['email']) ? 'value="' . e($fields['email']) . '"' : '' ?> /> </label> <label> <img src="img/message.png" alt="Message Icon">Messaggio* <textarea name="message" cols="20" rows="2"> <?php echo isset($fields['message']) ? e($fields['message']) : '' ?> </textarea> </label> <input type="submit" value="Invia" /> </form> </div> </section> 替换为whenGet

小心将http动词全部写成大写。

参见 ngMock documentation