我正在尝试在Angular 2中实现Dynamic Forms.我在动态表单中添加了删除和取消等附加功能.我已按照此文档:https://angular.io/docs/ts/latest/cookbook/dynamic-form.html
我对代码做了一些更改.我在这里得到错误.
我该怎么做这个错误?
你可以找到完整的代码在这里:http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview,这是在plunker但不是在我的本地系统的工作.
Html代码:
<div>
<form [formGroup]="form">
<div *ngFor="let question of questions" class="form-row">
<label [attr.for]="question.key">{{question.label}}</label>
<div [ngSwitch]="question.controlType">
<input *ngSwitchCase="'textbox'" [formControlName]="question.key"
[id]="question.key" [type]="question.type" [(ngModel)]="question.value">
<select [id]="question.key" [(ngModel)]="question.value" *ngSwitchCase="'dropdown'" [formControlName]="question.key" >
<option *ngFor="let opt of question.options" [ngValue]="opt.key" >{{opt.value}}</option>
</select>
<input *ngSwitchCase="'checkbox'" [(ngModel)]="question.value"
[id]="question.key" [type]="question.type" (change)="question.value = ck.checked" #ck [ngModelOptions]="{standalone: true}">
</div>
<div class="errorMessage" *ngIf="!form.controls[question.key].valid">{{question.label}} is required</div>
</div>
<div class="form-row">
<button type="submit" [disabled]="!form.valid" (click)="onSubmit()">Save</button>
<button type="button" class="btn btn-default" (click)="cancel()">Cancel</button>
<button type="button" class="btn btn-default" (click)="clear()">Clear</button>
</div> …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在用户点击链接时打开新页面.我不能使用Angular 2路由器,因为它没有任何功能可以重定向到外部URL.
所以,我正在使用window.location.href ="...";
HTML代码:
<button (click)="onNavigate()">Google</tn-submenu-link>
Run Code Online (Sandbox Code Playgroud)
打字稿代码:
onNavigate(){
//this.router.navigateByUrl("https://www.google.com");
window.location.href="https://www.google.com";
}
Run Code Online (Sandbox Code Playgroud)
但是如何在新标签中打开它?什么时候使用window.location.href?
我正在尝试将样式应用于子组件标记,但我不能这样做.
我有带锚标签的子组件.
即使我在父组件中有锚标签的样式,它也不适用.它有什么解决方案?
工作代码:http://plnkr.co/edit/CJCpV4ZbG7hdxT2AeSmt?p = preview
<a href="https://www.google.com">Google</a>
Run Code Online (Sandbox Code Playgroud)
在父组件中,我正在使用子组件并为此子组件应用样式.
Html代码:
<div class="container">
<div class="test">
<testapp></testapp>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Css代码:
.container{
font-family:sans-serif;
font-size:18px;
border: 1px solid black;
}
.test{
width:50%;
background-color:#f0f5f5;
}
.container:hover .test{
background-color:#e6ffe6;
}
.container:hover .test:hover{
background-color:#ffffe6;
}
.container .test a {
color: red ;
}
.container .test a:hover {
color:green;
}
Run Code Online (Sandbox Code Playgroud) 我在 ngOnInit 函数中有一个带有 setTimeOut 函数的组件。要为此编写单元测试用例,我使用 tick 和 fakeAsync 来快进 setTimeOut。但是,它不会被执行,而不会调用其他函数 closeAlert()。
组件代码:
export class BannerComponent implements OnInit {
@Input()errorData: any;
@Input()callback: any;
@Input()autoHide: boolean;
constructor() { }
ngOnInit() {
if (this.autoHide) {
setTimeout
(() => {
this.closeAlert();
}, 500);
}
}
closeAlert() {
this.errorData = null;
if (this.callback) {
this.callback();
}
};
}
Run Code Online (Sandbox Code Playgroud)
规范文件:
describe('BannerComponent', () => {
let component: BannerComponent;
let fixture: ComponentFixture<BannerComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BannerComponent ]
})
.compileComponents();
}));
beforeEach(async() => {
fixture = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现取消功能,该功能应该恢复到表单中的上一个值,这些值在上次提交表单时出现.我正在尝试创建一个传递给表单的数据对象的副本,在提交函数中我将新值替换为副本,在"取消"功能中,我将复制值替换为原始值.但是当我调用取消功能时,我没有得到以前的值.
带错误的工作代码:http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p = preview
我基于Angualr 2表单的模板驱动代码实现了取消功能:http://plnkr.co/edit/LCAPYTZElQDjrSgh3xnT?p = preview
Typescript类代码:
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { QuestionBase } from './question-base';
import { QuestionControlService } from './question-control.service';
@Component({
selector: 'dynamic-form',
templateUrl: 'app/dynamic-form.component.html',
directives: [REACTIVE_FORM_DIRECTIVES],
providers: [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {
@Input() questions: QuestionBase<any>[] = [];
form: FormGroup;
payLoad:object;
questiont: QuestionBase<any>;
constructor(private qcs: QuestionControlService) { }
ngOnInit() {
this.form = this.qcs.toFormGroup(this.questions);
console.log("Form Init",this.questions);
this.questiont=this.questions; …
Run Code Online (Sandbox Code Playgroud) angular ×5
css ×2
forms ×2
javascript ×2
typescript ×2
angular5 ×1
angular6 ×1
html ×1
unit-testing ×1