我不知道如何在 angular 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) 我是 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)
我怎样才能让这个轻松打开一个文件对话框?
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) 我side-nav在我的项目中使用组件在组件之间进行路由,如下所示:
在这里,我面临一个问题。如图所示,对于移动设备side-nav出现问题toggle-menu。当我click/select任何list-item (ex Home)。side-nav应该关闭。但它不是。
我已经问过一次了,仍然没有合适的解决方案。
这是stackblitz DEMO
我最近创建了一个在所有浏览器中运行良好的原生 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)我通过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)
对话框的内容总是向下滚动 - 接近结尾。如何防止此功能并从一开始就显示内容?
如何测试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 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) 设想 :
我正在使用角材料选项卡,每个不同的选项卡都有各自的动态组件。
因此,当我切换选项卡时,它会从 DOM 中删除内容。当我再次返回该选项卡时,它会再次加载内容。
问题:
我只想将内容更改为display:none;而不是从 DOM 中删除它,而不是默认行为。
angular6 ×10
angular ×6
html ×2
javascript ×2
angular7 ×1
components ×1
css ×1
observable ×1
promise ×1
routing ×1
typescript ×1
variables ×1