标签: angular6

角度转换 HttpClient 响应 - 类或接口

我看到了两种将响应转换为我们的自定义数据模型的方法。

界面

export interface User {
  id: number;
  name: string;
  more_complex_object: // so I am not sure how do this in right way. pass here another interface 
  //or just a object like {id: '', name: ''}. I show how to do this in class way...
}
Run Code Online (Sandbox Code Playgroud)

并在服务中:

return this.http.get<User>...
Run Code Online (Sandbox Code Playgroud)

没关系,不需要做更多的事情。

班级

export interface Deserializable {
  deserialize(input: any): this;
}

export class ComplexModel implements Deserializable {
  field1: string;
  field2: number;
  field3: number;

  deserialize(input: any): this {
    Object.assign(this, input);
    return this;
  } …
Run Code Online (Sandbox Code Playgroud)

angular angular5 angular6

6
推荐指数
2
解决办法
3440
查看次数

您可以在父路由器出口中显示您的子路由吗?

主要问题:有没有办法显示到父路由器出口的子路由?

请考虑Angular.io路由器指南中的这个示例。

主要路线片段:

  const appRoutes: Routes = [
      ...
      {
        path: 'admin',
        loadChildren: 'app/admin/admin.module#AdminModule',
        canLoad: [AuthGuard]
      },
      {
        path: 'crisis-center',
        loadChildren: 'app/crisis-center/crisis-center.module#CrisisCenterModule',
        data: { preload: true }
      },
      ....
    ];
Run Code Online (Sandbox Code Playgroud)

主要组件片段:

@Component({
  selector: 'app-root',
  template: `
    <h1 class="title">Angular Router</h1>
    <nav>
      <a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
      <a routerLink="/superheroes" routerLinkActive="active">Heroes</a>
      <a routerLink="/admin" routerLinkActive="active">Admin</a>
      ...
    </nav>
    <router-outlet></router-outlet>
  `
})
Run Code Online (Sandbox Code Playgroud)

危机中心路线片段:

const crisisCenterRoutes: Routes = [
  {
    path: '',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        children: [
          {
            path: ':id',
            component: …
Run Code Online (Sandbox Code Playgroud)

angular-routing router-outlet angular6

6
推荐指数
1
解决办法
4837
查看次数

@ngrx/store-devtools - 效果被调用两次

我在效果中调度的所有操作都会被调用两次。我已经弄清楚了,这是由 引起的StoreDevtoolsModule。当我退出时,StoreDevtoolsModule我app.module.ts的动作只会被调用一次=>一切都很好。

这是我的设置:

Angular 6.0.1
"@ngrx/effects": "6.0.0-beta.1",
"@ngrx/entity": "5.2.0",
"@ngrx/router-store": "6.0.0-beta.1",
"@ngrx/store": "6.0.0-beta.1",
"@ngrx/store-devtools": "6.0.0-beta.1",
Run Code Online (Sandbox Code Playgroud)

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { StoreModule, Store, combineReducers, ActionReducerMap, MetaReducer } 
from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { EffectsModule } from '@ngrx/effects';
import { EquipmentModule } from './modules/equipment/equipment.module';

const reducers: ActionReducerMap<IAppState> = {
    equipments: EquipmentReducer
};

@NgModule({

    declarations: [
        AppComponent
    ],
    imports     : [
        BrowserModule,
        StoreModule.forRoot(reducers),
        StoreDevtoolsModule.instrument({
            name: 'EQUIPMENT',
            maxAge: 10,
            logOnly: …
Run Code Online (Sandbox Code Playgroud)

ngrx-effects ngrx-store angular6

6
推荐指数
0
解决办法
956
查看次数

具有不同类的多个模块中相同 InjectionToken 的提供者

我有一个BaseComponent需要注入Service_A或Service_B使用InjectionToken:

export const SERVICE_TOKEN = new InjectionToken<Service_A | Service_B>('ServiceInjector');

...

export class BaseComponent {
   constructor(
     @Inject(SERVICE_TOKEN)
     public service: Service_A | Service_B
   ) { }
}
Run Code Online (Sandbox Code Playgroud)

有两个扩展的组件BaseComponent:Component_A和Component_B。对于他们每个人,我想注入他们相应的服务。

如果我在组件级别使用 DI 提供程序,则效果很好:

@Component({
  selector: 'ui-component-a',
  templateUrl: `../base.component.html`,
  providers: [{
    provide: SERVICE_TOKEN,
    useClass: Service_A
  }]
})
export class Component_A extends BaseComponent { }
Run Code Online (Sandbox Code Playgroud)
@Component({
   selector: 'ui-component-b',
   templateUrl: `../base.component.html`,
   providers: [{
     provide: SERVICE_TOKEN,
     useClass: Service_B
   }]
})
export class Component_B extends …
Run Code Online (Sandbox Code Playgroud)

dependency-injection angular angular6

6
推荐指数
0
解决办法
980
查看次数

PrimeNG Treetable 添加节点问题

我正在使用 Angular (6.0.9) 和 PrimeNG (6.0.1) 的最新稳定版本。当我尝试以编程方式将子节点添加到选定节点时,除非我折叠然后展开选定的父节点,否则它不会显示。首先我尝试过

this.selectedNode.children.push(this.nodeToAdd);
Run Code Online (Sandbox Code Playgroud)

然后我在官方文档中看到,如果“操作值,例如删除节点、添加节点或更改节点的子节点,而不是使用数组方法(例如push、splice),而是使用扩展运算符或类似操作创建新的数组引用”。所以我尝试了这个:

this.selectedNode.children = [...this.selectedNode.children, this.nodeToAdd];
Run Code Online (Sandbox Code Playgroud)

但它会导致相同的行为。有人面临同样的问题,还是我做错了什么?Chrome、Firefox 和 Edge 中均出现问题

primeng angular6

6
推荐指数
1
解决办法
2994
查看次数

PrimeNg context menu passing data issue

I am using PrimeNg's context menu v6.0.1, the problem is the documentation is not clear and I cannot find it over the web as well on how to pass data to command function e.g.:

I have 10 objects rendered on screen and context menu is attached to all of these objects, now if I click on menu item I want to get the id of the target object on which the context menu is rendered, how can this be accomplished? …

primeng angular angular6

6
推荐指数
1
解决办法
5341
查看次数

Angular 6服务注入Interceptor后未定义

我找不到任何方法将AuthService注入ErrorHandlerInterceptor中。它在注入后返回一个“未定义”对象,或者抛出一个错误。

这是我的ErrorHandlerInterceptor:

import { Injectable } from '@angular/core';
import { AuthService } from '@app/auth.service';
import { StorageService } from '@app/storage.service';

@Injectable({
  providedIn: 'root'
})
export class ErrorHandlerInterceptor implements HttpInterceptor {

  constructor(private authService: AuthService, private storageService: StorageService) {
    console.log(this.authService); // undefined
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(catchError(error => this.errorHandler(error)));
  }

  // Customize the default error handler here if needed
  private errorHandler(response: HttpErrorResponse): Observable<HttpEvent<any>> 
  {
    // ... Various code
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 …

angular angular6

6
推荐指数
2
解决办法
8274
查看次数

输入无线电中的 ngModel?

我正在使用 Angular 6 开发一个应用程序。我有一个大问题。使用模板驱动的表单,我希望当我按下提交按钮时可以发送单选按钮中选择的项目。<input type="text" [(ngModel)] = "value" />当我使用(value是我组件的数据字段)时没问题,但是如果我尝试这样做:

<div class="form-group">
    <div *ngFor = "let option of options">
    <div class="radio">
        <input type = "radio"
               name = "radio"
               [(ngModel)] = "value"
               />

        <label for="{{option.id}}">{{option.id}}</div>
        </label>

    </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

结果是bug!我什至无法通过移动选择器来单击多个按钮!一切都卡住了!显然它不适用于表格。如果我以[(ngModel)] = "value"图形方式删除它,它可以工作,但如果没有 ngModel 指令,如果我在使用 (ngSubmit) 的模板驱动表单中输入此代码,它就不起作用。多谢。

javascript frontend typescript angular6

6
推荐指数
2
解决办法
3万
查看次数

Angular 6:Reactive Form 的 updateOn:提交后“提交”不更新值

我正在使用带updateOn参数的Angular 反应式表单验证进行测试,但似乎即使是最简单的表单在我通过时也无法正常工作updateOn: 'submit'(其他值工作正常)。

app.component.html:

<form novalidate (submit)="onSubmit()">
  <input type="text" [formControl]="form.get('test')">

  Value: {{ form.get('test').value }}

  <button type="submit">SUBMIT</button>
</form>
Run Code Online (Sandbox Code Playgroud)

app.component.ts:

import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  public form = new FormGroup({
    test: new FormControl('', { validators: [Validators.required], updateOn: 'submit'}),
  });

  public onSubmit(): void {
    console.log('Submitted:');
    console.log(this.form.value); // Got: Object { test: "" }
  }
}
Run Code Online (Sandbox Code Playgroud)

完整和工作代码:https : //codesandbox.io/s/67pmd

单击按钮后我得到的是空字段值(在 …

typescript angular angular-reactive-forms angular6

6
推荐指数
1
解决办法
7750
查看次数

如何从 Angular 6 中的路由参数获取多个 ID?

我想获取我在使用 route.params 在 Angular 中路由时传递的多个 id。这是 route.ts

{path:'section/:id/:id', component: SubsectionsComponent}
Run Code Online (Sandbox Code Playgroud)

这就是我从 component.ts 路由的方式

onSectionClick(id1, id2){
    this.router.navigate(['/path/',id1,id2], {relativeTo:this.route})
  }
Run Code Online (Sandbox Code Playgroud)

这就是我在它路由的组件中获取的方式。

constructor(private route: ActivateRoute){}
this.route.params.subscribe(
      (route)=>{  
        console.log(route)
      }
    )
Run Code Online (Sandbox Code Playgroud)

但它只从参数中记录一个 id。

typescript angular angular6

6
推荐指数
1
解决办法
2469
查看次数