[英]How to inject service in angular js version more then 1.5x in the component?
我正在使用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'];
});
你可以尝试这样的事情吗?
(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])
您错过了控制器上的注射
函数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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.