我创建了一个使用DecimalPipe transform()方法的自定义管道。我在功能模块之一中使用此管道,并且必须将这两个管道都添加到其中providers: [](因为MyCustomPipe使用DecimalPipe),如下所示:
index.ts:
@NgModule({
imports: [
MaterialModule,
SharedModule
],
declarations: [
...
],
providers: [
DecimalPipe,
MyCustomPipe
...
Run Code Online (Sandbox Code Playgroud)
但是,我的目标是不必以这种方式将DecimalPipe添加到功能模块,并且不必在MyCustomPipe和DecimalPipe之间隐藏这种依赖关系,因此,使用MyCustomPipe的人可以担心从SharedModule导入MyCustomPipe。我尝试通过遵循SharedModule模式并从SharedModule导出DecimalPipe来解决此问题(就像我对MyCustomPipe所做的那样),如下所示:
shared.module.ts:
...import { DecimalPipe } from '@angular/common';
...export * from '../pipes/index';
@NgModule({
imports: [
CommonModule,
FormsModule,
HttpModule,
DecimalPipe
],
declarations: [
LoginComponent,
ErrorComponent,
MyCustomPipe,
],
exports: [
CommonModule,
HttpModule,
LoginComponent,
ErrorComponent,
DecimalPipe,
MyCustomPipe
]
})
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行此操作时,出现错误"Error: (SystemJS) Unexpected pipe 'DecimalPipe' imported by the module 'SharedModule'. Please add a @NgModule annotation."。现在,我可以declarations: []在SharedModule中添加DecimalPipe ,但是随后出现错误警告我在SharedModule和CommonModule中都声明了DecimalPipe。我认为这是由于我对文档中描述的SharedModule模式缺乏了解。即使这是正确的方法,我也不是100%,因为我从未尝试共享使用内置Angular管道和功能模块的自定义管道。
对于我们新的Angular应用程序,我的团队有兴趣使用Puppeteer进行E2E测试.在过去,我们使用了Protractor,它运作得相当好.我个人喜欢Puppeteer的"开箱即用",因为它与特定版本的Chromium无头捆绑在一起.这两个都符合我们的要求.我认为量角器更成熟,所以我想知道如果我们走下使用Puppeteer的道路,我们可能遇到什么问题.
我正在做一些关于在运行时更改Angular语言环境的研究,并找到了这个主题:如何在Angular 2中的DatePipe中设置语言环境?.
是否仍然无法在运行时更改语言环境?我有一个应用程序,它具有多个管道来根据语言环境格式化数字,日期和语言,但是为每个语言环境实现许多自定义管道 - 而不是在运行时能够更改它 - 似乎很荒谬.
你会用什么解决方案?
我可以通过像这样配置我的 .npmrc 来从我的内部/私有注册表发布和安装包:
$ npm config set registry https://mynpm-registry.com
Run Code Online (Sandbox Code Playgroud)
不过,我想我的.npmrc来代理了配置公共https://registry.npmjs.com/,如果包是不是在我的内部regisry可用。
我知道我可以配置多个 npm 配置文件 - 一个用于内部注册表,一个用于外部注册表 - 如下所示:
$ npmrc -c my-internal-profile
$ npmrc -c my-external-profile
Run Code Online (Sandbox Code Playgroud)
……但这不是我要找的。我想要一个 .npmrc 配置,包含两个注册表,如果我的内部注册表中没有可用的包,它将代理到公共注册表。
有没有办法做到这一点?
我是 Jasmine 的新手,我正在尝试编写一个简单的单元测试来检查我的方法是否返回预期值。这是我的 Angular 应用程序中的方法:
saveEvent(techEvent): Observable<IEvent>{
let headers = new Headers({ 'Content-Type': 'application/json'})
let options = new RequestOptions({headers: headers})
return this.http.post('/api/events', techEvent, options)
.map((response: Response) => {
//have an updated copy of saved event
return response.json()
}).catch(this.handleError)
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它是一个保存“techEvent”对象的 post 方法。这是我试图在规范中编写的测试:
it('should save the event', () => {
var testEvent = { id: 7, name: "AngularJS"}
mockHttp.post.and.returnValue(Observable.of(false))
techEventService.saveEvent(<IEvent>testEvent)
expect(techEventService.saveEvent).toBe(jasmine.any(Object))
})
Run Code Online (Sandbox Code Playgroud)
这个测试失败了:(。我的目标是简单地测试该方法是否返回一个对象并返回传递的特定对象。我想知道我是否也可以测试它是否是一个 JSON...
我正在尝试编写一种算法,该算法查找数组中的最小和最大值,第二大和第二小值。
我尝试了以下方法:
numbers = [2, 4, 9, 2, 0, 16, 24]
var largest = numbers[0];
var smallest = numbers[0];
for (var i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
else if(numbers[i] < smallest) {
smallest = numbers[i];
}
console.log(largest);
console.log(smallest);
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用,只是打印出数组...我在做什么错?
我最近开始使用 Puppeteer 进行 e2e 测试(我对 Selenium Web Driver 有一些经验),但在讨论 POM 模式的文档中找不到任何内容。是否有任何可用的示例如何在 Puppeteer 的 node/ES7 中正确实现它?
假设我有一个简单的脚本来测试页面的登录功能:
(async () => {
...
await page.goto(url, {
timeout: 5000
});
await page.waitFor('input[id=userId]');
await page.type('input[id=userId]', 'John Doe');
await page.type('input[id=password]', 'password1');
await page.click('button[type="submit"]');
await page.waitFor('p.success-msg');
...
}();
Run Code Online (Sandbox Code Playgroud)
通常,我们会有一个用于登录页面的页面对象模型。我将如何为上述页面创建一个基本的 POM 并将其与我的脚本集成?在这种环境下,您将如何在我的测试脚本中调用 POM?我会用import吗?我只是在寻找一个基本的“hello world”示例。
我正在尝试使用JavaScript为此编写算法,但是我得到的str.length不是函数...
function extractMiddle(str) {
var position;
var length;
if(str.length() % 2 == 1) {
position = str.length() / 2;
length = 1;
} else {
position = str.length() / 2 - 1;
length = 2;
}
result = str.substring(position, position + length)
}
extractMiddle("handbananna");
Run Code Online (Sandbox Code Playgroud) In the datepicker documentation there is an example of the popup calendar being controlled programatically by using the open() and close() methods like so:
<mat-form-field class="example-full-width">
<input matInput [matDatepicker]="picker" placeholder="Choose a date">
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<button mat-raised-button (click)="picker.open()">Open</button>
Run Code Online (Sandbox Code Playgroud)
One can also set the opened property to true/false like so:
<button mat-raised-button (click)="picker.opened = true">Open</button>
Run Code Online (Sandbox Code Playgroud)
I wonder if there is anyway to use this to get the calendar popup to stay permanently opened for the purpose of letting the user click …
javascript ×9
angular ×5
node.js ×2
algorithm ×1
arrays ×1
datepicker ×1
e2e-testing ×1
formatting ×1
jasmine ×1
locale ×1
npm ×1
puppeteer ×1
string ×1
testing ×1
unit-testing ×1