Angular 在提供者中注入 $rootScope
Angular injecting $rootScope in provider
我无法访问 provider.I 中的 $rootScope 看起来很多 angular 其他模块实现。这与我的实施方式相同。
怎么了?
它尝试将 urload 作为一个单独的函数(类似于其他 getValue 函数)它不起作用
错误是 $emit 未定义
define(['angularAMD'], function () {
var contextServiceModule = angular.module('contextService', []);
var contextService = function () {
var context = {};
this.$get = ['$rootScope', function ($rootScope) {
console.log($rootScope);
return function (){
return {
init: init,
getValue: getValue,
setValue: setValue,
urlLoad: function () {
$rootScope.$emit('onInit', {});/// error here
}
};
};
}];
this.init = function () {
return context;
};
this.getValue = function (key) {
var value = context[key] ? context[key] : null;
return value;
};
this.setValue = function (key, value) {
context[key] = value;
};
}
contextServiceModule.provider('contextService', contextService);
});
Angular 提供程序是在配置阶段创建的,$rootScope 直到应用 运行 阶段才可用。这里有几个 Angular 资源和一个类似的问题:
您不能将 $rootScope 注入提供程序 $get 函数,因为它尚不可用。但是,您可以在函数调用时手动注入。
this.$get = ['$injector', function ($injector) {
return function (){
return {
init: init,
getValue: getValue,
setValue: setValue,
urlLoad: function () {
var $rootScope = $injector.get('$rootScope');
console.log($rootScope);
$rootScope.$emit('onInit', {});/// error here
}
};
};
}];
这就是我让 $rootScope 将消息从 React.js
广播到 Angular
的方式。
如果您的 ng-view
不在正文中,请改用您的。
function $broadcast(event, args]) {
angular.element('body').injector().invoke([
'$rootScope',
'$timeout',
($rootScope, $timeout) =>
{
args.unshift(event);
$timeout(() => {
$rootScope.$broadcast.apply($rootScope, args);
});
}
]);
}
从这里得到:
https://www.bimeanalytics.com/engineering-blog/you-put-your-react-into-my-angular/
!!重要提示: 以下解决方案在缩小时不起作用。为此你需要做依赖注入,但是在配置阶段不能调用 $get
app.provider('someHandler', function() {
this.$get = function ($rootScope) {
function doBroadcast(words) {
$rootScope.$broadcast(words);
}
return {
shout: function (words) {
doBroadcast(words);
}
}
}
});
如果需要在config阶段使用可以按如下方式
app.config(['someHandlerProvider', function(someHandlerProvider) {
someHandlerProvider.$get().shout('listen up');
}
在 运行 期间你可以 someHandler.shout('listen up')
我无法访问 provider.I 中的 $rootScope 看起来很多 angular 其他模块实现。这与我的实施方式相同。 怎么了? 它尝试将 urload 作为一个单独的函数(类似于其他 getValue 函数)它不起作用
错误是 $emit 未定义
define(['angularAMD'], function () {
var contextServiceModule = angular.module('contextService', []);
var contextService = function () {
var context = {};
this.$get = ['$rootScope', function ($rootScope) {
console.log($rootScope);
return function (){
return {
init: init,
getValue: getValue,
setValue: setValue,
urlLoad: function () {
$rootScope.$emit('onInit', {});/// error here
}
};
};
}];
this.init = function () {
return context;
};
this.getValue = function (key) {
var value = context[key] ? context[key] : null;
return value;
};
this.setValue = function (key, value) {
context[key] = value;
};
}
contextServiceModule.provider('contextService', contextService);
});
Angular 提供程序是在配置阶段创建的,$rootScope 直到应用 运行 阶段才可用。这里有几个 Angular 资源和一个类似的问题:
您不能将 $rootScope 注入提供程序 $get 函数,因为它尚不可用。但是,您可以在函数调用时手动注入。
this.$get = ['$injector', function ($injector) {
return function (){
return {
init: init,
getValue: getValue,
setValue: setValue,
urlLoad: function () {
var $rootScope = $injector.get('$rootScope');
console.log($rootScope);
$rootScope.$emit('onInit', {});/// error here
}
};
};
}];
这就是我让 $rootScope 将消息从 React.js
广播到 Angular
的方式。
如果您的 ng-view
不在正文中,请改用您的。
function $broadcast(event, args]) {
angular.element('body').injector().invoke([
'$rootScope',
'$timeout',
($rootScope, $timeout) =>
{
args.unshift(event);
$timeout(() => {
$rootScope.$broadcast.apply($rootScope, args);
});
}
]);
}
从这里得到: https://www.bimeanalytics.com/engineering-blog/you-put-your-react-into-my-angular/
!!重要提示: 以下解决方案在缩小时不起作用。为此你需要做依赖注入,但是在配置阶段不能调用 $get
app.provider('someHandler', function() {
this.$get = function ($rootScope) {
function doBroadcast(words) {
$rootScope.$broadcast(words);
}
return {
shout: function (words) {
doBroadcast(words);
}
}
}
});
如果需要在config阶段使用可以按如下方式
app.config(['someHandlerProvider', function(someHandlerProvider) {
someHandlerProvider.$get().shout('listen up');
}
在 运行 期间你可以 someHandler.shout('listen up')