我正在使用MatPaginator组件,我正在试图弄清楚如何翻译这些标签(文档对此不够清楚).
我发现这篇文章显示了如何执行此操作,并按照以下步骤操作:
1 - 我创建了一个名为的文件custom-paginator.ts
并在其中放置以下内容:
import { MatPaginator, MatPaginatorIntl } from '@angular/material';
export class CustomPaginator extends MatPaginatorIntl {
constructor() {
super();
this.nextPageLabel = ' My new label for next page';
this.previousPageLabel = ' My new label for previous page';
this.itemsPerPageLabel = 'Task per screen';
}
}
Run Code Online (Sandbox Code Playgroud)
2 - 在app.module.ts
我说:
@NgModule({
// ...
providers: [
{
provide: MatPaginatorIntl,
useClass: CustomPaginator
}
]
})
export class AppModule
Run Code Online (Sandbox Code Playgroud)
但是,它根本不会改变任何东西.我错过了什么?
我在我的应用程序中使用了Material 2,但在这个问题中,我想用Input专门解决问题.
正如您在API Reference中看到的那样,有一个名为的属性绑定required
,在占位符中显示为星号.
所以,我想知道是否有办法检查表单控件是否在Angular中有一个特定的验证器,因为我真的不想为每个输入手动设置[required]="true/false"
我阅读了AbstractControl文档,但我没有找到任何相关内容.我遇到的hasError
方法(这具有讽刺意味的是没有记录无处 ......无论是在FormGroup也不FormControl也不AbstractControl),但是这不是我要找的.它只是检查表单控件是否有错误,但正如您可能已阅读过,我想检查控件是否有一些特定的验证器...
一些代码:
<md-input-container>
<input placeholder="Placeholder"
mdInput [formControl]="anyCtrl"
[required]="anyCtrl.hasValidator('required')"> <!-- something like this -->
</md-input-container>
Run Code Online (Sandbox Code Playgroud)
我希望这个问题足够清楚.提前致谢.
我有以下接口:
export interface Meta {
counter: number;
limit: number;
offset: number;
total: number;
}
export interface Api<T> {
[key: string]: T[];
meta: Meta; // error
}
Run Code Online (Sandbox Code Playgroud)
目前,我收到以下错误:
'Meta'类型的属性'meta'不能赋予字符串索引类型'T []'.
搜索了一下之后,我在TS文档中找到了这个声明:
虽然字符串索引签名是描述"字典"模式的有效方式,但它们还强制所有属性都与其返回类型匹配.这是因为字符串索引声明obj.property也可用作obj ["property"].
这是否意味着当我有一个字符串索引签名时,我不能没有匹配此类型的任何其他变量?
实际上我可以摆脱这个错误声明这样的界面:
export interface Api<T> {
[key: string]: any; // used any here
meta: Meta;
}
Run Code Online (Sandbox Code Playgroud)
这样做,我失去了类型推断的完全能力.没有这种丑陋的方式,有没有办法做到这一点?
考虑以下组件:
@Component({
selector: 'app-test'
template: 'Hello!'
}}
export class TestComponent {
@Output() readonly selectionChange = new EventEmitter<SomeTypeHere>();
}
Run Code Online (Sandbox Code Playgroud)
随着电话:
<app-test (selectedChange)="selectedChangeHandler($event)"></app-test>
Run Code Online (Sandbox Code Playgroud)
请注意,我写的selectedChange
不是正确的输出名称selectionChange
。strictTemplates
启用标志的 Angular 9根本没有帮助我。它默默地失败了。有趣的是,如果我对 做同样的事情@Input
,应用程序会捕获错误并且不会编译。
如果我试图“听”一个不存在的东西,有什么办法可以抛出错误@Output
吗?
我有以下路由配置:
const routes: Routes = [
{
component: MyComponent,
path: '',
children: [
{
component: MyListComponent,
path: ''
},
{
component: MyFormComponent,
path: ':id/edit'
},
{ path: '**', redirectTo: '' }
]
}
];
Run Code Online (Sandbox Code Playgroud)
为了将参数限制为数字,我开始搜索并花费太多时间后,我已经找到了一种方法在这个封闭的(我不知道为什么)问题:https://github.com/angular/angular /问题/ 12442
所以,我将配置(特别是重要的部分)更改为以下内容:
{
component: MyFormComponent,
matcher: ComplexUrlMatcher('id', /[1-9]+\/edit/)
}
export function ComplexUrlMatcher(paramName: string, regex: RegExp) {
return (
segments: UrlSegment[],
segmentGroup: UrlSegmentGroup,
route: Route) => {
const parts = [regex];
const posParams: { [key: string]: UrlSegment } = {};
const consumed: …
Run Code Online (Sandbox Code Playgroud) 我有一个array
来自我的API而且我正在使用Material2#AutoComplete过滤这个...它到目前为止工作,但是我很难显示另一个属性而不是选项中的绑定值.
我知道我必须使用displayWith
它,但它不能像我期望的那样工作.调用的函数[displayWith]="displayFn.bind(this)">
只返回我id
,如何获取完整的对象,然后返回name
on函数.
顺便说一句,我仍然想在我的FormControl中绑定id .
一些代码:
零件:
export class AutocompleteOverviewExample {
stateCtrl: FormControl;
filteredStates: any;
states = [
{
id: 1,
name: 'Alabama'
},
{
id: 2,
name: 'North Dakota'
},
{
id: 3,
name: 'Mississippi'
}
];
constructor() {
this.stateCtrl = new FormControl();
this.filteredStates = this.filterStates('');
}
onKeyUp(event: Event): void {
this.filteredStates = this.filterStates(event.target.value);
}
filterStates(val: string): Observable<any> {
let …
Run Code Online (Sandbox Code Playgroud) 我正在使用响应者gem来干掉我的控制器.这是我目前的代码:
class OfficehoursController < ApplicationController
def new
@officehour = Officehour.new
end
def create
@officehour = Officehour.create(officehour_params)
respond_with(@officehour, location: officehours_path)
end
def officehour_params
params.require(:officehour).permit(:end, :start, :status)
end
end
Run Code Online (Sandbox Code Playgroud)
我现在面临的问题是:
当我发送有效参数时create
,它会重定向到officehours/
预期,但是当我得到422(验证错误)时,它会将URL更改officehours/new
为officehours/
(但它保留在表单页面... idk为什么).编辑/更新操作也是如此.
所以,我想留在.../new
或.../edit
当我得到422错误时,我该怎么办呢?
我使用@ @角度9.0.7,@ngneat/spectator@5.3.1
(用玩笑),Inputmask@5.0.3
在一个项目中,一切工作的应用程序时,我跑ng serve
,甚至ng build
,但是当我尝试运行测试套件的失败@Pipe
使用Inputmask
:
@Pipe
:
import { Pipe, PipeTransform } from '@angular/core';
import Inputmask from 'inputmask';
@Pipe({
name: 'appSomePipe',
})
export class SomePipe implements PipeTransform {
transform(value: string): string {
return Inputmask.format(value, {
jitMasking: true,
mask: '1111-1',
});
}
}
Run Code Online (Sandbox Code Playgroud)
@Spec
:
import { createPipeFactory, SpectatorPipe } from '@ngneat/spectator/jest';
import { SomePipe } from './some.pipe';
describe('SomePipe', () => {
let spectator: SpectatorPipe<SomePipe>;
const …
Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义验证器来验证我的唯一性FormArray
.我想在特定(s)值已经在数组中时显示错误.
问题是它没有按预期工作.
实际行为:
重现步骤:
预期行为:
如果"X组"中显示相同的值,则其特定输入必须显示错误.
在上述情况下,错误应出现在输入1和3上.
假设我有4个输入:
4个输入必须显示错误,因为它们都是重复的.
static uniqueBy = (field: string, caseSensitive = true): ValidatorFn => {
return (formArray: FormArray): { [key: string]: boolean } => {
const controls = formArray.controls.filter(formGroup => {
return isPresent(formGroup.get(field).value);
});
const uniqueObj = { uniqueBy: true };
let found = false;
if (controls.length > 1) {
for (let i = 0; i < controls.length; i++) {
const …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的服务,内容如下:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class AddressService {
constructor(private http: Http) { }
getAnything = (): Observable<Response> => {
return this.http.get('https://my_api.com')
.map(this.handleSuccess)
.catch(this.handleError);
}
handleError = (error: Response): Observable<Response> => {
return Observable.throw(error || 'Server Error');
}
handleSuccess = (response: Response): Observable<Response> => {
let body;
if (response.text()) {
body = response.json();
}
return body || {}; …
Run Code Online (Sandbox Code Playgroud) angular ×8
typescript ×4
angular-test ×1
angular9 ×1
autocomplete ×1
input-mask ×1
interface ×1
observable ×1