我有这个函数来检查password和confirmPassword字段是否具有相同的值。如果没有,该表格将被标记为invalid
confirmPasswordValidator(passwordGroupForm: AbstractControl){
let password = passwordGroupForm.get('password');
let confirmPassword = passwordGroupForm.get('confirmPassword');
console.log("password is "+password +"and confirmPassword is "+confirmPassword);
console.log("confirm password touched",confirmPassword.touched );
if(!confirmPassword.touched) return null ;//don't mark form invalid if the user hasn't interacted with confirmPassword field
else {
return (password.value === confirmPassword.value) ? null : {
confirmPasswordValidator: {
valid: false,
message: 'Password and Verify Password fields do not match'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我编写了以下测试用例来检查该功能是否有效。
it('should not submit form if password and confirm password are …Run Code Online (Sandbox Code Playgroud) 组件.ts
// Component containing function
Public myData(){
this.myService.getData()
.pipe(
take(1),
catchError((err: any) => {
this.myRows = [];
return throwError(err);
}),
finalize(() => {
console.log('In Finally');
})
)
.subscribe((rows) => {
this.myRows = rows;
// Do something
});
}
Run Code Online (Sandbox Code Playgroud)
myService.ts
// Service Call
public getData(): Observable < customer[] > {
const url = 'some url';
return this.http.get(url).pipe(
map(this.checkForError),
catchError((err: HttpErrorResponse) => {
return throwError(err);
}),
map(this.myJson),
share()
);
}
Run Code Online (Sandbox Code Playgroud)
规格
// Test Case
it('should test', () => {
let test = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 Angular 6 项目,我想使用 Chart.js 创建一些图表。
我使用 npm install Chart.js 安装了 Chart.js,其版本类似于 3.5.1。
我的 TypeScript 版本是 2.9.0。
所以当我尝试ng serve:
node_modules/chart.js/types/animation.d.ts(20,37): error TS1005: ',' expected.
node_modules/chart.js/types/animation.d.ts(20,46): error TS1005: ',' expected.
node_modules/chart.js/types/index.esm.d.ts(39,56): error TS1005: ';' expected.
node_modules/chart.js/types/index.esm.d.ts(39,58): error TS1011: An element access expression should take an argument.
node_modules/chart.js/types/index.esm.d.ts(39,85): error TS1005: '(' expected.
node_modules/chart.js/types/index.esm.d.ts(535,38): error TS1005: ',' expected.
node_modules/chart.js/types/index.esm.d.ts(535,56): error TS1005: ',' expected.
node_modules/chart.js/types/index.esm.d.ts(2424,37): error TS1005: ',' expected.
node_modules/chart.js/types/index.esm.d.ts(2424,50): error TS1005: ',' expected.
node_modules/chart.js/types/index.esm.d.ts(2734,63): error TS1005: ';' expected.
node_modules/chart.js/types/index.esm.d.ts(2734,69): error TS1011: …Run Code Online (Sandbox Code Playgroud) 所以我有这个简单的组件:
import {
Component,
ViewEncapsulation,
OnInit,
} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'custom-element',
template: '<div>{{ chuckText$ | async }}</div>',
styleUrls: ['./button.component.css'],
encapsulation: ViewEncapsulation.Native
})
export class ButtonComponent implements OnInit {
public chuckText$ = new BehaviorSubject<string>('');
public constructor(
private http: HttpClient
) {
this.chuckText$.next('Default Text');
}
ngOnInit() {
// nothing too fancy, just for testing purpose
this.http.get('https://api.chucknorris.io/jokes/random')
.subscribe((data: any) => {
console.log(data.value);
this.chuckText$.next(data.value);
});
}
}
Run Code Online (Sandbox Code Playgroud)
构建我的组件并将其添加到一个简单的angularJS应用程序后,(我需要将一个自定义元素添加到旧应用程序)我在控制台中使用默认文本和http.get结果获取我的元素,但我的组件尚未被更新,如图所示.
我显然遗漏了一些对我来说不太明显的事情.有关为什么内容不更新的任何想法?作为提醒,这个功能已经发布,但处于实验状态,也许就是这样.
作为参考,这是我的app.module.ts:
import …Run Code Online (Sandbox Code Playgroud) 我将Angular 6.0.5与Bootstrap 4.1.1一起使用,并且为下拉菜单添加了指令。但是我无法使其完全无效。我在这里看到了很多类似的问题,但Bootstrap 4却没有。这是下拉HTML:
<div class="btn-group" appDropdown>
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Manage
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" style="cursor: pointer"
(click)="OnAddToShoppingList()">To Shopping List</a></li>
<li><a class="dropdown-item" href="#">Edit Recipe</a></li>
<li><a class="dropdown-item" href="#">Delete Recipe</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
这是指令:
import {Directive, HostBinding, HostListener} from '@angular/core';
@Directive({
selector: '[appDropdown]'
})
export class DropdownDirective {
@HostBinding('class.open') isOpen = false;
@HostListener('click') toggleopen() {
this.isOpen = !this.isOpen;
}
}
Run Code Online (Sandbox Code Playgroud)
我怀疑“公开”课。而且我认为这是Bootstrap 3中已弃用的类,那么Bootstrap 4中的替代方法是什么?
我是Angular的新手(我正在使用Angular 6),我想在布局视图中定义一些“插入内容块”。
例如:
<!-- layout.component.html -->
<div>{{my.title.component}}</div>
<div>{{my.body.component}}</div>
Run Code Online (Sandbox Code Playgroud)
然后,我希望能够执行以下操作:
<!-- title.component.html -->
<h1>I am the title</h1>
Run Code Online (Sandbox Code Playgroud)
对于身体:
<!-- body.component.html -->
<p> I am a very complex component inserted up there </p>
Run Code Online (Sandbox Code Playgroud)
然后,我想创建如下内容:
<!-- assembled.component.ts -->
@MagicalDecorator({
layout: LayoutComponent,
use_as_title_component: TitleComponent,
use_as_body_component: BodyComponent,
}
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事吗?我可能使用了错误的方法,但这就是我想象的那样。如果有更好的方法,我也想知道。
编辑: 我的想法是我希望能够使用布局组件添加格式(例如,将它们放在引导面板中),而不必为要使用相同布局的每个组件组合重复相同的格式
我在应用程序中使用@ ng-select / ng-select @ 2.3.6,并且数组中有很长的文本。
因此,完整的文本在下拉列表中不可见,因此我想在每个选项上显示标题/工具提示
我试过了,
let listArray = [{name: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s'}];
<ng-select placeholder="Select" (change)="onChange($event)">
<ng-option *ngFor="let list of listArray"
title="{{list.name}}"> {{list.name}} </ng-option>
</ng-select>
Run Code Online (Sandbox Code Playgroud)
但是没有运气
<div class="form-group" [formGroup]="gGroup">
<label for="grouplabel">Group</label>
<select formControlName="groupControl" [(ngModel)]="groupobj" (ngModelChange)="onSelectGroup($event)" id="group" class="form-control">
<option>Select</option>
<option *ngFor="let group of groups" [selected]="current_group === group.name">{{group.name}}</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
如何在组件中将<select>字段的值设置为“ 动态选择 ”?
我看了看文档https://angular.io/api/forms/FormControlName#use-with-ngmodel,但仍然没有可以帮助我的示例。
尝试:
this.gGroup.get('groupControl').setValue('Select');
如标题所述,我们是否需要为模块手动实现延迟加载,还是Angular 6/7默认执行?
我想使用ngIf更改在迭代中显示的名称,我正在尝试执行以下操作:
*ngIf="detail.name=='dieselG_d'? detail.name='Diesel Green':Diesel Red
Run Code Online (Sandbox Code Playgroud)
并且控制台记录了以下错误:
Uncaught Error: Template parse errors:
Run Code Online (Sandbox Code Playgroud) angular6 ×10
angular ×8
angular-test ×1
angular7 ×1
bootstrap-4 ×1
chart.js ×1
jasmine ×1
unit-testing ×1