我无法想象我需要使用工厂提供商的情况.
根据官方文档https://angular.io/docs/ts/latest/guide/dependency-injection.html,情况是人们可能无法从另一个服务(服务)中访问服务(service-b)-a),但是,工厂功能确实(可以访问service-b).那么,什么时候真的会发生这样的事情呢?
我们是大型 Angular 应用程序模块的开发人员。模块彼此独立,由不同的团队开发。我们想在我们的模块中使用 ngrx 存储。
另一个模块已经有一个 ngrx 存储。如果我尝试以通常的方式将商店添加到我们的 module.ts 中:
@NgModule({
imports: [
...
StoreModule.provideStore( ... )
],
...
Run Code Online (Sandbox Code Playgroud)
它打破了整个应用程序。有没有办法为我们的模块提供一个单独的存储?
(应用程序使用 ngrx2)
我正在使用 Angular 插件,需要通过使用InjectionToken插件导出的配置对象来配置它。
import { pluginToken } from 'plugin';
@NgModule({
providers: {
// Configure the plugin
//
// The configuration value string needs to be taken from some other
// provider (available for dependency injection).
{ provides: pluginToken, useValue: valueString },
},
})
class MyModule {
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是这valueString是来自其他提供商的价值。我不知道如何将依赖项注入@NgModule装饰器的提供者。怎么做?
我正在编写一个规范来检查在测试的Angular模块的配置阶段调用方法.
这是对正在测试的代码的简化看法:
angular.module('core',['services.configAction'])
.config(function(configAction){
configAction.deferIntercept(true);
});
Run Code Online (Sandbox Code Playgroud)
上面发生的是我们定义一个core具有单一依赖关系的模块.
然后,在模块的config-block中core,我们调用给定使用deferIntercept的configAction对象上的方法services.configAction.
我正在尝试测试core配置调用该方法.
这是当前的设置:
describe('core',function()
{
const configActionProvider={
deferIntercept:jasmine.createSpy('deferIntercept'),
$get:function(){
return {/*...*/}
}
};
beforeEach(function()
{
module(function($provide)
{
$provide.provider('configAction',configActionProvider);
});
module('core.AppInitializer');
inject(function($injector)
{
//...
});
});
it('should call deferIntercept',function()
{
expect(configActionProvider.deferIntercept).toHaveBeenCalledWith(true);
});
});
Run Code Online (Sandbox Code Playgroud)
问题是它没有覆盖configAction,因此从不调用间谍,原始方法是.
如果我将它作为core模块的依赖项删除它将会这样做,因此angular.module('core',[])代替angular.module('core',['services.configAction'])将工作并调用间谍.
有services.configAction没有想过如何在测试期间覆盖而不从依赖列表中删除它?
只是想确保我理解它的语义@Injectable(providedIn: 'root').在Angular 6之前,如果我们从包含服务的NPM导入模块,我们将在我们的app模块中声明该模块,以便整个应用程序可以访问该服务.像这样的东西:
import { SomeNPModule } from '@ngx/SomeNPModule';
@NgModule({
imports: [
BrowserModule,
SomeNPModule
]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
现在我们可以注入SomeService模块提供的内容,因为我们已经导入了模块.使用Angular 6,因为我们正在使用服务本身,所以需要导入SomeNPModule到其中,并且当注释运行时,它会自动使注释容器中的服务可用吗?AppModule@Injectable(providedIn: 'rootroot
所以我们有了答案,但我认为它是部分完整的,因为如果我们想要配置服务,这通常是通过forRoot服务模块上的方法完成的......就像通过Router.因此,假设我们不想配置服务,我们需要做的只是注入它,但如果我们想要一个配置的服务,我们需要遵循这种Router模式.如果我在评论中犯了任何错误,请纠正我.
typescript angular-providers progressive-web-apps angular angular-dependency-injection
我正在尝试在 firebase 中托管我的应用程序,在 localhost 中一切正常,但是当我在 firebase 域的 firebase 中成功托管应用程序时,它显示:
NullInjectorError: StaticInjectorError(wo)[class{constructor(t,e) at SpinnerService at content is not loaded.
我试图在提供者数组中插入提供ngxModule和ngxSpinnerService,但我仍然有同样的问题。
我的代码:app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule,FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {NgxPaginationModule} from 'ngx-pagination';
import { CommonModule } from '@angular/common';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { HttpClientModule …Run Code Online (Sandbox Code Playgroud) 我必须测试一个组件并收到此错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]: NullInjectorError: No provider for Router!**" 我正在测试的组件有两个依赖项,但我没有知道 hot 在测试中使用 testbed gallery.component.ts提供它们
constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){
this.iterableDiffer = iterableDiffers.find([]).create(null);
}
Run Code Online (Sandbox Code Playgroud)
画廊.component.spec.ts
describe('GalleryComponent', () => {
let component: GalleryComponent;
let fixture: ComponentFixture<GalleryComponent>
let authServiceMock: AuthenticationService
let iterableDifferMock: IterableDiffers
beforeEach(() => {
TestBed.configureTestingModule({
providers:[GalleryComponent, {provide:AuthenticationService}, {provide:IterableDiffers}]
})
fixture = TestBed.createComponent(GalleryComponent);
component = fixture.componentInstance;
authServiceMock = TestBed.inject(AuthenticationService)
})
...
Run Code Online (Sandbox Code Playgroud)
如何提供这两个依赖项?
我阅读了 Angular DOC,但没有找到解决方案,我在 SO 上提出了其他问题,但没有找到解决方案。
谢谢
我为社区编写一个库,它需要访问表单控件并监听更改的值。
这是我之前使用 Angular 2.3 和 4.0 时的做法。
export class ValidationMessageDirective implements OnInit {
@Input('validationMessage') customMessage: string;
constructor(private el: ElementRef, private formControl: NgControl, private validationMessageService: ValidationMessageService) {
this.target = $(el.nativeElement);
this.formGroup = this.target.closest('.form-group');
this.formControl.valueChanges.subscribe((newValue) => {
this.checkValidation();
});
}
}
Run Code Online (Sandbox Code Playgroud)
但升级到 Angular 4.1 后,此代码不再起作用。这是我得到的错误:
ERROR Error: Uncaught (in promise): Error: No provider for NgControl!
有什么建议或想法让我聆听指令中更改的值吗?
更新1:
这是我的index.ts
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ValidationMessageDirective …Run Code Online (Sandbox Code Playgroud) angular-directive angular-providers angular-validation angular
Angular 提供程序文档指出:
当Angular路由器延迟加载模块时,它将创建一个新的注入器。这种注射器是根应用注射器的孩子。想象一棵注入器树;每个懒加载模块有一个根注入器,然后有一个子注入器。路由器将所有提供程序从根注入器添加到子注入器。当路由器在延迟加载的上下文中创建组件时,Angular首选从这些提供者创建的服务实例,而不是应用程序根注入器的服务实例。
在延迟加载的模块上下文中创建的任何组件(例如通过路由器导航)都将获取服务的本地实例,而不是根应用程序注入器中的实例。外部模块中的组件继续接收为应用程序根目录创建的实例。
这是否意味着当我在延迟加载的模块中访问任何全局声明的提供程序时,便会访问它的副本,该副本与在根注入器中创建的实例是分开的?
假设我有2种情况:
AppModule
ProviderXAppComponent
ProviderXSubpageModule
SubpageComponent
ProviderXAppModule
ProviderXAppComponent
ProviderXSubpageModule
ProviderXSubpageComponent
ProviderX在情况A中,ProviderXin 的实例与in 的实例SubpageComponent相同AppComponent还是不同?我了解情况B中的情况并非如此。
我有一个名为MyPageModule导入多个模块的复杂模块,它为 Service 提供以下注释@Injectable( { providedIn: 'root' } )。
这个模块是通过延迟加载导入的,如下所示:
// AppRoutingModule
...
{
path: 'my-page',
loadChildren: './lazy-loader-modules/lazy-loader-mypage/lazy-loader-mypage.module#LazyLoaderMyPageModule'
}
...
Run Code Online (Sandbox Code Playgroud)
// LazyLoaderMyPageModule
@NgModule({
declarations: [],
imports: [
CommonModule,
MyPageModule
]
})
export class LazyLoaderMyPageModule { }
Run Code Online (Sandbox Code Playgroud)
我想要的行为(实际情况并非如此): 当 url 与 /my-page/* 不同时,我希望 MyPageModule 导入的所有服务都被销毁。
我怎样才能做到这一点 ?
angular ×9
typescript ×2
unit-testing ×2
angular-dependency-injection ×1
angularjs ×1
firebase ×1
jasmine ×1
ngrx ×1
ngrx-store ×1