标签: angular6

使用 @ContentChildren() 访问每个 ng-template 名称

我编写了一个名为的组件my-component,并在里面添加了一些组件ng-template,并为每个组件命名,#如下所示:

<my-component>
    <ng-template #one>
        1st format
    </ng-template>
    <ng-template  #two>
        2nd template
    </ng-template>
    <ng-template  #three>
        3rd template
    </ng-template>
</my-component>
Run Code Online (Sandbox Code Playgroud)

在 的声明中MyComponent,我曾经@ContentChildren(TemplateRef)访问过这三个模板。

现在我需要以某种方式在内部访问这些模板的名称(one、、),但我不知道如何。这是代码:示例代码twothreeMyComponent

angular-directive ng-template angular angular6

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

如何在 Angular 6 中观察服务变量

我已经尝试让 Angular 更新服务变量“open_popup”好几天了,但没有成功。

太长了;我想做的是,每当变量“open_popup”发生变化时,使其在组件上发生变化,也能够从组件函数中更改服务变量。

这是我到目前为止所尝试过的。

应用程序/src/app/audience-creator/shared/audience-creator.service.ts

import { Injectable } from "@angular/core";
import {Observable} from "rxjs/index";
import { of } from "rxjs/index";

@Injectable()
export class AudienceCreatorService {
    constructor(private http: HttpClient) {}

    public open_popup = false;

    public toggleCreatorPopup() {
        this.open_popup = !this.open_popup;
    }

    popupIsOpen(): Observable<any> {
        return of(this.open_popup);
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序/src/app/audience-creator/audience-creator.component.ts

import {Stuff} from "@angular/core";

@Component({
    selector: 'audience-creator',
    templateUrl: 'audience-creator.html'
})
export class AudienceCreatorComponent implements AfterViewInit, OnInit {

    public popup_is_open = false;

    @Input()
    audience_creator_service;

    ngOnInit() {
        this.audience_creator_service.popupIsOpen().subscribe(
            popup_is_open => this.popup_is_open = …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular6

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

在路由解析器中使用 NGRX

我正在使用 Angular 6。我也在使用 NGRX Store。我正在使用路由防护来确保用户登录到应用程序。然后,我使用解析器获取初始用户配置文件,然后将其放入 NGRX 存储中。

我是 NGRX 的新手,我不确定这是否是编写解析器的正确方法。

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
     return this.loginService.getLoginData()
        .pipe(
            map((result:UserData) => { 
                this.store.dispatch(new userActions.SetLoginData(result)); 
                this.loginService.getDropdownData(
                    result.userId, 
                    result.countryCode,
                ).subscribe( data => { 
                    this.store.dispatch(new userActions.SetDropdownData(data));
                })
            })
        )
}  
Run Code Online (Sandbox Code Playgroud)

我也不确定这是否是执行 RXJS 的正确方法。

任何建议,谢谢

rxjs ngrx angular6

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

如何为angular 6 bootstrap 4 modal做茉莉花单元测试用例

html

<ng-template #content let-modal>   
<h1>Modal content inside this ng-template #content </h1>  
</ng-template>
Run Code Online (Sandbox Code Playgroud)

按钮打开模型

<button  (click)="open(content)" > Open modal </button>
Run Code Online (Sandbox Code Playgroud)

在 ts 文件中

import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
constructor(  public modalService: NgbModal) { }

open(content) {  
          this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title', size: 'lg' }).result.then((result) => {
            this.closeResult = `Closed with: ${result}`;
          }, (reason) => {
            this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
          });
           }
Run Code Online (Sandbox Code Playgroud)

如何为这个开放函数做一个茉莉花测试用例。

jasmine karma-jasmine bootstrap-4 ng-bootstrap angular6

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

如何在 Angular 6 中显示以 base64 格式编码的图像?

我正在努力解决与 Angular 6 相关的问题并显示以 base64 格式编码的图像。任何想法为什么下面显示的代码不显示图像?

html:

<tr *ngFor="let row of this.UploaderService.tableImageRecognition.dataRows">
<img src="{{this.hello}}" />
Run Code Online (Sandbox Code Playgroud)

ts:

this.hello = "data:image/png;base64, /9j/4AAQSkZJRgABAQAAA..."
Run Code Online (Sandbox Code Playgroud)

虽然下面显示的代码可以正常工作并显示图片?

html:

<tr *ngFor="let row of this.UploaderService.tableImageRecognition.dataRows">
<!--<img src="data:image/png;base64, /9j/4AAQSkZJRgABAQAAA..."/>
Run Code Online (Sandbox Code Playgroud)

this.hello在构造函数中分配只是为了测试目的。我以这种方式创建它this.hello = 'data:image/png;base64, ' + this.UploaderService.imageRecognitionRowsData[0].toString()我的主要目标是imageRecognitionRowsData在这个循环中显示<tr *ngFor="let row of this.UploaderService.tableImageRecognition.dataRows">。因此,对于第一次迭代,我将显示图像,imageRecognitionRowsData[0]然后在下一次迭代图像imageRecognitionRowsData[1]等期间显示图像。的长度this.UploaderService.tableImageRecognition.dataRows始终与this.UploaderService.imageRecognitionRowsData添加时相同,我<p>{{this.hello}}</p>在 html 中得到相同的字符串。我不知道出了什么问题。我也试图与this.hello2 = this.sanitizer.bypassSecurityTrustResourceUrl(this.hello);,this.hello2 = this.sanitizer.bypassSecurityTrustUrl(this.hello);,<img [src]="this.hello" />等但没有任何工程。任何想法如何解决这个问题?

html base64 typescript angular angular6

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

Angular mat-select 获取选中的选项数据对象

在我的Angular 7应用程序下,我使用的是 mat-select 组件:

<mat-form-field  class="col-md-3" color="warn">
     <mat-select placeholder="Selectionner le code Edo"
                  id="codeEdo"
                  [(value)]="selectedCodeEdoValue"
                  ngDefaultControl
                  formControlName="codeEdo">
     <mat-option (onSelectionChange)="onEdoSelectionChanged(edo )"
                 *ngFor="let edo of shopsList"
                  [value]="edo.edoId">
                  {{edo.edoId}}
     </mat-option>
     </mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

我的选项数据如下:

let shopsList= [
  {
    'edoId': '4010',
    'storeName': 'ABBEVILLE'
  }
  ,
  {
    'edoId': '3650',
    'storeName': 'AGEN'
  }
  ,
  {
    'edoId': '3298',
    'storeName': 'AIX ALLEES PROVENCALES'
  }
];
Run Code Online (Sandbox Code Playgroud)

我的目的是在选择一个选项时获取选定的对象数据,而不仅仅是值

例如,当我选择第一个选项时,我想得到这个:

result = {
    'edoId': '4010',
    'storeName': 'ABBEVILLE'
}
Run Code Online (Sandbox Code Playgroud)

不仅是值“4010”

正如你所看到的,我试过这个: (onSelectionChange)="onEdoSelectionChanged(edo )

但这总是激发我两个事件 …

javascript angular-material angular-material2 angular angular6

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

“查看 DOM”和“内容 DOM”有什么区别?

@ViewChild和@ViewChildren查询从视图DOM和元素@ContentChild和@ContentChildren从内容DOM查询的元素。

@ViewChildren(WriterComponent) 
writers: QueryList<WriterComponent>;    

@ContentChildren(WriterComponent) 
writers: QueryList<WriterComponent>
Run Code Online (Sandbox Code Playgroud)

在这里我无法理解视图 DOM 和内容 DOM 之间究竟有什么区别,请有人解释一下吗?

angular angular6

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

如何在 Angular 6 应用程序中使用 React 组件

我有一些反应组件,我想在 angular 6 应用程序中重用相同的组件。可以来一些建议我最好的方法来做到这一点。

reactjs angular angular6

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

当字段未触及时,Angular 反应形式返回空值

在我的用例中,当用户单击编辑按钮时,Angular 对后端进行 HTTP 调用并检索对象,然后在 EDIT 表单上填充这些值。用户可以更新或保持字段不变。当单击update按钮时,Angular 应该获取表单中存在的所有这些值并将它们发送到后端。所以,这是问题所在,在将值加载到编辑页面表单并更新某些字段并保留某些字段不变后,未修改的值会变为空。这真的很奇怪

产品-edit.component.html

<div *ngIf="productDataAvailable()">
  <h2>Update Product</h2>
  <br/>
  <form [formGroup]="productForm">
    <div class="form-group">
      <label for="id">Product Id</label>
      <input class="form-control" formControlName="id" id="id" type="text" value="{{product.id}}">

      <small class="form-text text-muted" id="emailHelp"></small>
    </div>
    <div class="form-group">
      <label for="name">Name</label>
      <input class="form-control" formControlName="name" id="name" type="text" value="{{product.name}}">
    </div>
    <div class="form-group">
      <label for="description">Description</label>
      <input class="form-control" formControlName="description" height="100" id="description" required type="text"
             [value]="product.description" width="200">
    </div>
    <div class="form-group">
      <label for="currency">Price</label> <br/>
      <label>
        <select (load)="loadCurrencies()" class="form-control"  [value]="product.price.currency.symbol" formControlName="currency" id="currency" name="currency">
          <option *ngFor="let currency of currencies" value={{currency.id}}>
            {{currency.name}}
          </option>
        </select> …
Run Code Online (Sandbox Code Playgroud)

angular angular-httpclient angular6 angular7

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

如何计算 .ts 文件中的总行数

我有 app 文件夹,并在该数量的模块和组件下。我只需要计算 .ts 文件的代码行数。那么是否有任何 VS Code 插件或任何其他方式请建议?

typescript angular6

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