【问题标题】:How to inject service in angular js version more then 1.5x in the component?如何在组件中注入超过 1.5 倍的 Angular js 版本的服务?
【发布时间】:2018-11-07 18:22:21
【问题描述】:

我正在使用 angular js 1.5x 版本,我正在使用组件执行此操作,我已经编写了服务并尝试将服务注入控制器中,但我遇到了一些错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.5.7/$injector/unpr?p0=MyServiceProvider%20%3C-%20MyService

这是我的代码:

 (function () {
//var app = angular.module("teleconsultantVideo");

app.component('teleVideo', {
    bindings: { accessToken: '@' },
    templateUrl: './teleconsultant-video/teleconsultant-video.component.html',
    controller: TeleconsultantVideoController,
    controllerAs: 'ctrl',


});


function TeleconsultantVideoController($scope) {
    var ctrl = this;
    $ctrl.$onInit = function () {
        alert("Hi");
    }      
        MyService.loadSecurityMatrix($scope.accessToken)
        $scope.onCallDisconnect = true;        

}

app.controller("TeleconsultantVideoController", TeleconsultantVideoController);
TeleconsultantVideoController.$inject = ['$scope', 'MyService'];
})();

我的服务:

(function () {
// var app = angular.module("teleconsultantVideo");

function MyService($http, $q) {

    function loadSecurityMatrix(token) {

        alert("into the function");
        var authToken = token;
        var config = { 'headers': { 'Authorization': "Bearer " + authToken } };
        return $http.get('http://localhost:3000/securityMatrix', config).then(function (result) {
            // this._loggedInCustomer = this.mapToLocal(response);
            return result;
        }, function (err) {
            return err;
        });
    }
}

app.service('MyService', MyService);
MyService.$inject = ['$http', '$q'];

 });

【问题讨论】:

    标签: javascript angularjs angular-components


    【解决方案1】:

    你能试试这样的吗?

      (function() {
        'use strict';
    
        angular
            .module('moduleName')
            .factory('yourService', Service);
    
        function Service($http, $rootScope, $q,$window) {
            return {
                //YOUR METHODS
            }
        }
        })();
    
    
    
     ////// ***YOUR CONTROLLER ***///////////
        function CtrlName($scope,yourService) {
            //CODE HERE
        };
        angular
            .module('moduleName')
            .controller('CtrlName', ['$scope','yourService',CtrlName])
    

    【讨论】:

    • 我的服务文件是否正常
    • 我认为是的,您是在 index.html 中包含服务文件还是使用延迟加载?
    【解决方案2】:

    你错过了控制器上的注入

    函数 TeleconsultantVideoController($scope)

    ->function TeleconsultantVideoController($scope, MyService)

    function TeleconsultantVideoController($scope) {
        var ctrl = this;
        $ctrl.$onInit = function () {
            alert("Hi");
        }      
            MyService.loadSecurityMatrix($scope.accessToken)
            $scope.onCallDisconnect = true;        
    
    }
    

    到这里

    function TeleconsultantVideoController($scope, MyService) {
        var ctrl = this;
        $ctrl.$onInit = function () {
            alert("Hi");
        }      
            MyService.loadSecurityMatrix($scope.accessToken)
            $scope.onCallDisconnect = true;        
    
    }
    

    【讨论】:

    • 你还需要加载你的服务脚本@Debashish dwivedi
    • 好的,我会试试的
    猜你喜欢
    • 2018-05-20
    • 2016-08-08
    • 2016-10-27
    • 2016-10-01
    • 2016-05-05
    • 2015-09-05
    • 2020-02-27
    • 2016-08-04
    • 2016-09-15
    相关资源
    最近更新 更多