我看待它的方式,用模型定义对象似乎使它们变得僵硬并且变化容忍度更小,从而使得将来更容易破坏应用程序并添加代码行而无法获得.例如,我可以有一个get方法,它从API返回一个视频对象,并将其编程为Any或定义的模型.
/model/video.ts
export interface Video {
// my code
}
Run Code Online (Sandbox Code Playgroud)
/pages/videos.ts
getAllVideos(): Promise<Video> {
// my code
}
Run Code Online (Sandbox Code Playgroud)
与
/pages/videos.ts
getAllVideos(): Promise<Any> {
// my code
}
Run Code Online (Sandbox Code Playgroud)
我看待它的方式.更少的代码行,更少的复杂性,更少的文件和更少的刚性是一件好事.为什么要定义模型?
我有一个服务,在构造函数中,我正在运行一种方法来获取一些数据。我可以看到数据,然后将其传递给预定义的变量。
像这样:
export class SentinelService {
configuration;
constructor() {
this.electronService.ipcRenderer.on('config' , function(event , data) {
this.configuration = data; // pass the data to configuration variable
console.log(this.configuration.name); // Check it ... I can see the value here
});
}
myMethod() {
// Try it here
console.log(this.configuration.name); // I get Undefined
};
...
Run Code Online (Sandbox Code Playgroud)
尽管我已经将值分配给“ configuration”变量,并且可以看到它是从构造函数内部的方法传递的,但是当我在另一个方法上尝试相同的操作时,却无法定义。
我怎样才能解决这个问题?
我创建了一个实现httpinterceptor的角度拦截器.它适用于http GET请求.但是没有POST请求.
我的拦截器代码如下.
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders,HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
constructor() { console.log('enter constructor');
}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
console.log('entered interceptor')
const token = localStorage.getItem('sessiontoken') != null ? localStorage.getItem('sessiontoken') : 'notoken';
const authReq = req.clone({ headers: req.headers.set("Authorization", token) });
return next.handle(authReq);
}
}
Run Code Online (Sandbox Code Playgroud)
该模块添加了提供程序.
providers: [
{
provide: HTTP_INTERCEPTORS,
useValue: { disableClose: true, …Run Code Online (Sandbox Code Playgroud) 我们$scope.$apply()在angularjs中执行了正确的angularjs范围生命周期。
Angular 6中有与此等效的东西吗?
正如我在标题中提到的那样,我正在尝试验证用户输入的手机号码是否是印度号码。
为此,我从印度手机号码的正则表达式中引用了一种模式
但就我而言,它总是返回false。
我想验证以下内容。
app.component.html
<form class="example-form" [formGroup]="userAddressValidations">
<mat-form-field appearance="outline" class="col-sm-6">
<mat-label>10-digit Mobile Number</mat-label>
<input matInput formControlName="mobileNumber" maxlength="10" (keypress)=_keyPress($event)>
<mat-error *ngIf="userAddressValidations.get('mobileNumber').hasError('required')">
Please fill out this field.
</mat-error>
<mat-error *ngIf="userAddressValidations.get('mobileNumber').hasError('pattern')">
It is not a valid mobile number.
</mat-error>
</mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)
app.component.ts
export class InputErrorStateMatcherExample {
userAddressValidations: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.userAddressValidations = this.formBuilder.group({
mobileNumber: ['', [Validators.required, Validators.pattern('^[6-9]\d{9}$')]]
});
}
_keyPress(event: any) {
const pattern = /[0-9]/;
let inputChar = String.fromCharCode(event.charCode);
if (!pattern.test(inputChar)) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建跨越多个组件的Reactive-Form,就像这样
<form [formGroup]="myForm" (ngSubmit)="onSubmitted()">
<app-names></app-names>
<app-address></app-address>
<app-phones></app-phones>
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
当我尝试提交时,我得到空对象.
typescript angular angular-reactive-forms angular-forms angular6
我正在学习Angular 6,只是想将一些我学到的东西放到togheter上,我目前遇到了一个我找不到答案的问题。我正在尝试使用* ngFor更改LI的样式,具体取决于索引是First,Last,Odd还是Even。到目前为止,一切正常,但是我不知道如何为“ Last”做这件事,因为我将所有新对象添加到列表中时,它显然是最后一个,因此它呈现了最后一个颜色。
我知道该怎么做,但真正的问题是我正在从表单中动态地将内容添加到列表中,但我不确定如何评估“ Last”以使其他颜色变为正确的颜色。
请记住,我仍然是个新手,看起来可能很凌乱,而且我也了解,自HTMl5以来,我正在进行的某些客户端验证可能不是最佳的或不需要的,但我还是要学习它。
这是我的组件HTML代码
>
<h1>List of courses :</h1><br>
<div *ngIf="courses.length > 0; then coursesList else noCourses"></div>
<ng-template #coursesList>
<h2>List of Courses :</h2>
<ul *ngFor="let course of courses; index as i;">
<li [ngStyle]="{'background-color':getColor(i)}" style="color: white;">
<strong>Index : </strong>{{i}} <strong>ID : </strong>{{course.id}} <strong>Name</strong> : {{course.name}}
<button (click)="onRemove(i)">Remove</button>
<button (click)="onModify(i)">Modify</button>
</li>
</ul>
</ng-template>
<ng-template #noCourses>
<h5>There are no courses in this list. Use the form bellow to add some.</h5>
</ng-template>
<div (keyup.enter)="onAdd()">
<span>ID : <input type="number" (keypress)="checkNumber($event)" [(ngModel)]="fields.id" …Run Code Online (Sandbox Code Playgroud) 我有一个userService.spec.ts要测试的文件。代码就是这样。它调用了一个get函数,该函数调用了ADMIN_API_URL。我尝试测试给定的文件,但标题中提到了错误。我的user.service.spec.ts文件。
describe('Service: User service', () => {
let httpMock: HttpTestingController;
let userService: UserService;
let url: string;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ provide: AUTH_API_URL, useValue: 'https://auth.example.com/api/'
},
{ provide: SSO_API_URL, useValue: 'https://sso.example.com/auth/api/' },
{ provide: WIT_API_PROXY, useValue: 'https://wit.example.com/api/'},
{ provide: ADMIN_API_URL, useValue: 'https://admin.example.com/api/'},
{ provide: REALM, useValue: 'realm' },
Broadcaster,
Logger,
AuthenticationService,
HttpHandler,
UserService
]
});
httpMock = TestBed.get(HttpTestingController);
url = TestBed.get(ADMIN_API_URL);
userService = TestBed.get(UserService);
});
it('Get user by user name …Run Code Online (Sandbox Code Playgroud) 一个问题:
<button (click)="activateMotion(1)">
<img class="emotion-icon" id="positive-icon" src="" />
</button>
<button (click)="activateMotion(-1)">
<img class="emotion-icon" id="negative-icon" src="" />
</button>
Run Code Online (Sandbox Code Playgroud)
我有两个按钮,其值为图像.我想写一个函数调用:activateMotion().我想在值1或-1的基础上更改按钮内的图像.我可以使用jQuery来实现这个目标,但我正在Angular中编写应用程序.
我的应用程序网站在IE11上无法正常工作。
网站未加载,给我空白页
这是在IE11上引发的错误:
SCRIPT5005: String expected
js (26,286)
SCRIPT5022: Exception thrown and not caught.
polyfills.js (3234,3)
Run Code Online (Sandbox Code Playgroud)
我将屏幕截图封装在控制台上
我已经将API包含在其中my index.html:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
Run Code Online (Sandbox Code Playgroud)
它应该在我的polyfills.js之后包含googlemaps,但是我不确定如何做到这一点
我尝试使用agm-map和其他解决方案,但到目前为止,没有任何效果。
angular6 ×10
angular ×7
typescript ×5
angular7 ×2
angular5 ×1
angularjs ×1
arrays ×1
google-maps ×1
javascript ×1
jquery ×1
unit-testing ×1