Как внедрить услугу в угловой версии JS более 1,5x в компоненте? - PullRequest
0 голосов
/ 29 мая 2018

Я работаю с угловой версией 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'];

 });

Ответы [ 2 ]

0 голосов
/ 29 мая 2018

вы пропускаете инъекцию на вашем контроллере

функция TeleconsultantVideoController ($ scope)

-> функция 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;        

}
0 голосов
/ 29 мая 2018

Можете ли вы попробовать что-то подобное?

  (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])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...