У меня есть два модуля «core» и «ui».Переменная watch service от другого модуля внутри директивы
Модуль ui зависит от ядра. Это код для моего core.js:
var core = angular.module('core', [ 'ngRoute' ]);
//Services
core.service('httpInformationService', function() {
this.requestCount = 0;
this.responseCount = 0;
this.incrementRequest = function() {
this.requestCount++;
console.log('incrementRequest:' + this.requestCount);
};
this.incrementReponse = function() {
this.responseCount++;
}
this.decrementRequest = function() {
this.requestCount--;
console.log('decrementRequest:' + this.requestCount);
};
this.decrementResponse = function() {
responseCount--;
}
this.getRequestCount = function() {
return requestCount;
}
this.getResponseCount = function() {
return responseCount;
}
});
//Service provider
core.provider("httpServiceInformationProvider", function() {
var provider = {};
provider.$get = ['httpInformationService', function(service) {
return service;
}];
return provider;
});
//HTTP Interceptor
core.factory('coreHttpInterceptor' ,function(httpInformationService){
var coreHttpInterceptor = {
request: function(config) {
httpInformationService.incrementRequest();
return config;
},
response: function(response) {
httpInformationService.decrementRequest();
return response;
}
}
return coreHttpInterceptor;
});
var config = {
base_url: enviromnent_url,
}
core.value('config', config);
core.config(function($interpolateProvider) {
$interpolateProvider.startSymbol("[[").endSymbol("]]");
});
core.config(function($httpProvider) {
$httpProvider.interceptors.push('coreHttpInterceptor');
});
Это мой ui.js код:
var ui = angular.module('ui',[ 'core' , 'ui.bootstrap' ]);
ui.directive("shLoadify" , function(httpServiceInformationProvider){
return {
restrict: "AE",
link: function(scope, element, attrs) {
element.bind("click", function() {
element.text("Loading...");
element.prop("disabled", true);
});
},
controller: function($scope) {
$scope.$watch('httpServiceInformationProvider', function(oldValue, newValue){
console.log(oldValue + ' ' + newValue);
}, true);
}
}
});
Как вы можете видеть, я пытаюсь получить доступ requestCount свойства httpInfomationService внутри моего контроллера с помощью $ scope.watch.
Проблема: newValue и oldValue всегда равны нулю. Почему это так?
Вы смотрите '$' scope.httpInfomationService и неопределен – k102
Когда я пытаюсь войти, он не является. – iamjc015
Нет, вы регистрируете что-то внутри службы. где вы помещаете это в свой объем? – k102