angular7 一个简单的自定义管道实现(给数字加百分号)

@Pipe({

name: 'myPipe'

})

export class MyPipePipe implements PipeTransform {

transform(size: number, extension: string = '%'): any {

return this.toNumberFormat(size) + extension;

}

public toNumberFormat(x) {

let formatNumberValue = parseFloat(x);

if (isNaN(formatNumberValue)) {

return;

}

formatNumberValue = Math.round(x * 100) / 100;

return formatNumberValue;

}

}

使用:

<span>{{numberValue | myPipe}}</span>

 

这样得到的值都是除100加"%"号,并且保留2位小数.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单Angular画面的示例代码: ``` import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <div> <h1>Welcome to my Angular App!</h1> <p>{{ message }}</p> <button (click)="updateMessage()">Update Message</button> </div> ` }) export class AppComponent { message = 'This is a message!'; updateMessage() { this.message = 'The message has been updated!'; } } ``` 这是一个非常简单的画面,只有一个标题,一个消息和一个按钮。当用户单击按钮时,消息会被更新。 为了实现自动化测试,我们可以使用Angular的测试类。以下是一个简单的测试用例: ``` import { TestBed, ComponentFixture } from '@angular/core/testing'; import { AppComponent } from './app.component'; describe('AppComponent', () => { let component: AppComponent; let fixture: ComponentFixture<AppComponent>; beforeEach(() => { TestBed.configureTestingModule({ declarations: [ AppComponent ] }); fixture = TestBed.createComponent(AppComponent); component = fixture.componentInstance; }); it('should create the app', () => { expect(component).toBeTruthy(); }); it('should update the message', () => { component.updateMessage(); expect(component.message).toEqual('The message has been updated!'); }); }); ``` 在这个测试用例中,我们首先使用TestBed创建一个测试模块,并声明AppComponent。然后,我们使用createComponent方法创建一个AppComponent实例,并保存在fixture变量中。 接下来,我们编写两个测试用例。第一个测试用例测试AppComponent是否被正确创建,它应该返回true。第二个测试用例测试updateMessage方法是否能够正确更新消息。 我们可以使用Angular CLI运行这些测试用例,并获得自动化测试结果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值