簡體   English   中英

UnknownError:JavaScript錯誤:[$ injector:unpr]未知提供程序

[英]UnknownError: javascript error: [$injector:unpr] Unknown provider

嘗試為我的角度應用程序創建量角器測試,這是我嘗試從Jasmine腳本調用的服務:

var app =angular.module('app',[]);

app.factory('myService', function($http) {
        var myService = {
            async: function() {
                // $http returns a promise, which has a then function, which also returns a promise
                var promise = $http.get('test.json').then(function (response) {
                    // The then function here is an opportunity to modify the response
                    console.log(response);
                    // The return value gets picked up by the then in the controller.
                    return response.data;
                });
                // Return the promise to the controller
                return promise;
            }
        };
        return myService;
    });

;

我的規范是這樣定義的:

it('should call api i the site', function () {
    browser.executeAsyncScript(function(callback) {
    var callback = arguments[arguments.length - 1];
    var myService = angular.injector(['ng']).get('myService');
    myService.async().then(function (d) {
            $scope.data = d;
    });
    callback(null, true);
    });
})

當我運行腳本時,包含angular.injector(['ng'])。get('myService')的行會導致以下錯誤:

UnknownError: javascript error: [$injector:unpr] Unknown provider: myServiceProvider <- myService

正確的噴油嘴說明是什么?

試試這個

var injector = angular.injector(['app', 'ng']);
var myService = injector.get('myService');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM