我只想知道如何重新渲染 ngFor 或刷新组件(hello.component.ts)。代替文本可以是图像或图表。
这是一个简单的例子:Stackblitz
解决方案:
public show = true;
addData(data){
this.data.push(data);
this.reload();
}
reload() {
this.show = false;
setTimeout(() => this.show = true);
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何在 Angular 中使用 Jasmin/Karma 测试分支。例如我有这个简单的功能:
loadData(){
if(this.faktor){ // here it should be true or false
this.callMethod1();
}else{
this.callMethod2();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要增加测试覆盖率,并且需要测试分支。我尝试使用下面的示例,但它不起作用。我需要将 this.factor.isExist() 设置为 true。我该怎么做?
这是我的测试组件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ChartComponent } from './chart.component';
describe('ChartComponent', () => {
let component: ChartComponent;
let fixture: ComponentFixture<ChartComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ChartComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ChartComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call …Run Code Online (Sandbox Code Playgroud) 我正在使用 json-server 并且出现以下错误。我做错了什么?
类型错误:无法在 Function.createId (/Users/Picchu/Documents/url/node_modules/json-server/lib/server/mixins.js:47:39) [0] 处读取 Function.createId 处未定义 [0] 的属性“id”[0] .insert (/Users/Picchu/Documents/url/node_modules/lodash-id/src/index.js:47:49) [0] 在 /Users/Picchu/Documents/url/node_modules/lodash/lodash.js:4388 :28 [0] 在 arrayReduce (/Users/Picchu/Documents/url/node_modules/lodash/lodash.js:683:21) [0] 在 baseWrapperValue (/Users/Picchu/Documents/url/node_modules/lodash/lodash. js:4387:14)
createShortUrl(data: ShortUrl): Observable<any> {
let params = new HttpParams();
params = params.append('url', 'http://google.com');
return this._http.post(`${'/api'}`, { params: params }).pipe(map((res) => {
return res;
}
Run Code Online (Sandbox Code Playgroud) 你好,
我正在使用 Angular Material,我创建了一个表单字段,我想验证我的 url,但它不起作用。我尝试使用电子邮件,它正在工作。我还希望在输入有效网址之前禁用该按钮:
这是我的代码:
export class StartComponent implements OnInit {
searchValue: string;
url: any;
public reg = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
constructor() { }
ngOnInit() {
this.url = new FormControl('', [Validators.required, Validators.pattern(this.reg)]);
this.getErrorMessage();
}
getErrorMessage() {
return this.url.hasError('required') ? 'You must enter a value' :
this.url.hasError('email') ? 'Not a valid url' :
'';
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<mat-card class="searchbox">
<mat-form-field>
<input matInput placeholder="Enter your url" [formControl]="url" required>
<mat-error *ngIf="url.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
<!--Here button should be disabled if url not valid and until there …Run Code Online (Sandbox Code Playgroud)