标签: angular6

如何在 Angular 6/5/4 中没有父子关系的情况下将组件 A 之间的数据传递给组件 B

我不知道如何在 angular 6 的两个组件之间传递数据或变量,我接受任何建议,请帮助我

variables components angular angular6

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

如何在角度 6 的单选按钮中执行验证?

实际上我有两种类型的输入类型:一种是普通文本,另一种是单选类型。我想在输入提交按钮之前验证这两个字段。

在此处输入图片说明

对于这个普通的输入字段,我进行了验证,但我不知道如何为单选按钮做。我想验证用户是否没有选择任何单选按钮。

<form  name="form" #f="ngForm" (ngSubmit)="f.form.valid && onSubmit()" novalidate class="feedback-form">
         <div class="col-md-12">

            <div class="form-group col-md-4">
               <label for="selectionDate">Selection Date</label>
               <input type="text" 
               id="selectionDate"
               class="form-control"
                name="selectionDate"
                placeholder="Please enter the date" 
               [(ngModel)]="selection.selectionDate"
                #selectionDate="ngModel"
                [ngClass]="{ 'is-invalid': f.submitted && selectionDate.invalid }"
                 required
                  />

             <div *ngIf="f.submitted && selectionDate.invalid" class="invalid-input">
            <!-- individual validation errors -->
            <div *ngIf="selectionDate.errors?.required">DateField is required</div>
          </div>

            </div>
            <div class="form-group col-md-4">
               <label for="selectedBy">Selected By</label>
               <br>
               <label class="radio-inline">
               <input type="radio" name="selectedBy" 
               [(ngModel)]="selection.selectedBy" value="Department">Department
               </label>
               <label class="radio-inline">
               <input type="radio" name="selectedBy"  
               [(ngModel)]="selection.selectedBy" value="Office">Office
               </label>
            </div>
         </div>
</form>
Run Code Online (Sandbox Code Playgroud)

javascript angular angular6

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

在 Angular 6 中单击图像时如何打开文件对话框

我是 Angular 6 的新手。我知道这可能很愚蠢,但我没有得到任何解决方案。单击文本字段中的浏览图像时,我想打开文件对话框。这是我尝试过的。

<div class="boxed">
  <div class="input-group">
      <input id="spFile" class="form-control" name="spFile">
    <img src="../../../../../assets/images/maps/Browse.png" type= "file" width="40" height="40" style=" position: absolute; top: 1px; right: 1px" aria-hidden="true"/>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我怎样才能让这个轻松打开一个文件对话框?

html css angular6

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

当 skiplocationchange 为真时返回 Angular 6

Angular 6 中的路由

当我路由到特定位置时,我会导航

route.navigate(['/home'], { skipLocationChange: true });
Run Code Online (Sandbox Code Playgroud)

但是当返回到以前的路线时,下面的代码没有帮助,有没有其他方法或应该删除“{skipLocationChange:true}”

import {Component} from '@angular/core';
import {Location} from '@angular/common';

@Component({
  // component's declarations here
})
class SomeComponent {

  constructor(private _location: Location) 
  {}

  backClicked() {
    this._location.back();
  }
}
Run Code Online (Sandbox Code Playgroud)

html javascript routing angular angular6

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

在单击小型设备的路由器链接时关闭侧边导航

我side-nav在我的项目中使用组件在组件之间进行路由,如下所示:

在此处输入图片说明

在这里,我面临一个问题。如图所示,对于移动设备side-nav出现问题toggle-menu。当我click/select任何list-item (ex Home)。side-nav应该关闭。但它不是。

我已经问过一次了,仍然没有合适的解决方案。

这是stackblitz DEMO

angular-routing angular-material angular angular6

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

在 Angular 6 中扩展原生 HTML 元素

我最近创建了一个在所有浏览器中运行良好的原生 Web 组件。我将此 Web 组件移动到 Angular 6 应用程序中,并且一切正常。然后我尝试扩展一个原生 HTML 元素,该元素再次完美运行,除非我将它带入我的 Angular 6 应用程序。

使用 Mozilla 的示例,我将尝试说明我的问题。使用以下尝试扩展本机“p”元素:

// Create a class for the element
class WordCount extends HTMLParagraphElement {
  constructor() {
    // Always call super first in constructor
    super();

    // count words in element's parent element
    var wcParent = this.parentNode;

    function countWords(node){
      var text = node.innerText || node.textContent
      return text.split(/\s+/g).length;
    }

    var count = 'Words: ' + countWords(wcParent);

    // Create a shadow root
    var shadow = this.attachShadow({mode: 'open'});

    // Create text …
Run Code Online (Sandbox Code Playgroud)

web-component custom-element angular6

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

如何防止在 Angular 材质对话框中向下滚动?

我通过http请求检索数据,文本被预先格式化为 HTML 文本,而且大部分时间都很长。基本上我使用以下内容:

<h2 matDialogTitle> My title</h2>
<mat-dialog-content>
  <p [innerHTML]="data.displayText" ></p>
</mat-dialog-content>
<div mat-dialog-actions align="end">
  <button mat-button mat-dialog-close color="primary">Close</button>
</div>
Run Code Online (Sandbox Code Playgroud)

对话框的内容总是向下滚动 - 接近结尾。如何防止此功能并从一开始就显示内容?

angular6 angular-material-6

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

如何对Angular的canActivate进行单元测试?

如何测试canActivateangular 的函数,该函数返回一个函数,该函数又返回一个boolean值?。

我尝试创建ActivatedrouterSnapshotand 的对象routerStateSnapshot并将其传递给canActivate函数,但这没有帮助。

export class AuthGuard implements CanActivate {
constructor(
private authService: AuthenticationService,
private loginService: LoginService,
private router: Router
) {}

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> 
| boolean {
return this.checkLogin(state.url);
}

checkLogin(url: string): boolean {
    if (this.authService.isLoggedIn()) {  return true; }

// Store the attempted URL for redirecting
this.loginService.redirectUrl = url;

// Navigate to the login page with extras
this.router.navigate(['/login']);
return false;
 }
}
Run Code Online (Sandbox Code Playgroud)

自从 …

angular-ui-router angular angular6 angular7

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

使用 Observable 或 Promise 在 Angular 6 中转换管道

我试图在 angular 6 中设置管道,使用服务将文本转换为其他(返回可观察的方法)

我尝试了以下代码,但我需要返回一个字符串而不是 Promise

管道:

import { Pipe, PipeTransform } from '@angular/core';
import { TimeZoneService, TimeZone } from '../services/Global/timezone.service';
//import { resolve } from 'dns';
import { reject } from 'q';
import { Observable } from 'rxjs';

@Pipe({
  name: 'utcToText'
})
export class UtcToTextPipe implements PipeTransform {

  private timezoneLst: TimeZone[] = [];

  constructor(private _timezoneSvc : TimeZoneService) {}

  async transform(timezone: any, args?: any){
    this.timezoneLst = await this._timezoneSvc.getTimeZonesLst().toPromise();
     return this.timezoneLst.find(x => x.utc.indexOf(timezone) > -1).text;

}
}
Run Code Online (Sandbox Code Playgroud)

html:

<span>{{subscription.time_zone |  utcToText}</span> …
Run Code Online (Sandbox Code Playgroud)

promise observable typescript angular angular6

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

mat-tab 删除 tab-body DOM 但不隐藏它

设想 :

  • 我正在使用角材料选项卡,每个不同的选项卡都有各自的动态组件。

  • 因此,当我切换选项卡时,它会从 DOM 中删除内容。当我再次返回该选项卡时,它会再次加载内容。


问题:
我只想将内容更改为display:none;而不是从 DOM 中删除它,而不是默认行为。

angular-material2 angular6 angular-material-6

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