小编med*_*d.b的帖子

Angular 4 将数据/参数传递给模态(使用 ngfor)

我是 angular 4 的新手,我正在使用 ngFor 在数组中显示数据。每个插件都有许多用户和我设法从后端获取的这些用户的列表(id、role 等)(spring boot 项目) ).我想要做的是显示这些用户的数量,当用户点击显示数量的按钮时,会弹出一个模式并显示这些用户的详细信息。所以我遇到的问题是如何将 {{addon.something}} 传递给模态。

       <tbody>
            <tr *ngFor="let addon of addons">
                <td>{{addon.name}}</td>
                <td>{{addon.url}}</td>
                <td>{{addon.location}}</td>
               <td>
                 <button class="btn btn-outline-primary" (click)="open(content,addon)" >{{addon.users.length}}</button>
                 <!--{{addon.users.length}}-->
                </td>

                <td>
                    <a routerLink="/assign_user_to_addon/{{addon.id}}">Add user</a>
                </td>
            </tr>
        </tbody>
Run Code Online (Sandbox Code Playgroud)

我试图将它传递给(click)="open(content,addon)",但它不起作用。

用于处理模态的打字稿代码:

 open(content:any,addon:any) {
    this.modalService.open(content).result.then((result) => {

      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) …
Run Code Online (Sandbox Code Playgroud)

typescript bootstrap-modal ngfor angular

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

Angular 6,在服务中调用函数时“无法读取未定义的属性”

我试图在服务中调用一个函数,但我一直有 Cannot read property 'authenticationService' of undefined

我已经authenticationService在组件的构造函数中初始化,

组件:

 export class RegisterComponent implements OnInit {
  error: string;
  isLinear = true;
  registerFormGroup: FormGroup;
  loading = false;
  form: FormGroup;
  studentRegister: any;
  samePassword = false;
  hide = true;
  hide1 = true;
  matcher = new MyErrorStateMatcher();

constructor(
   private router: Router,
   private _formBuilder: FormBuilder,
   private http: HttpClient,
   private i18nService: I18nService,
   private authenticationService: AuthenticationService
) {
}

ngOnInit() {
    this.registerFormGroup = this._formBuilder.group({
    first_name: [null, Validators.compose([Validators.required,                 Validators.minLength(3), Validators.maxLength(20)])],
    last_name: [null, Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(20)])],
    email: [null, …
Run Code Online (Sandbox Code Playgroud)

http-post typescript angular-services angular angular-httpclient

4
推荐指数
1
解决办法
4265
查看次数

Angular单元测试TypeError:this.http.get(...).pipe不是函数

我正在按照这个例子对服务进行单元测试(来自spring app后端的get请求)https://angular.io/guide/testing#testing-http-services

服务类:

@Injectable()
export class TarifService {

  constructor(private messageService: MessageService, private http: HttpClient) { }

  public getTarifs(): Observable<Tarif[]> {
    return this.http.get<Tarif[]>(tarifffsURL).pipe(
      tap(() => {}),
      catchError(this.handleError('getTarifs', []))
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

单元测试

describe('TarifService', () => {

  let tarifService: TarifService;
  let httpClientSpy: { get: jasmine.Spy };
  let expectedTarifs;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ToastrModule.forRoot(), HttpClientModule, HttpClientTestingModule],
      providers: [TarifService, HttpClient, MessageService]
    });

    httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
    tarifService = new TarifService(<any> MessageService,<any> httpClientSpy);

  });


  it('should be created', inject([TarifService], (service: TarifService) => {
    expect(service).toBeTruthy(); …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine rxjs typescript angular

3
推荐指数
1
解决办法
7859
查看次数