Angular实现简单计算器
step1: D:\vue\untitled2904\src\app\app.component.ts
import {
Component} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'untitled2904';
equation = '0';
isDecimalAdded = false;
isOperatorAdded = false;
isStarted = false;
//输入+ - x /
isOperator(character: any) {
return ["+", "-", "×", "÷"].indexOf(character) > -1;
}
// operators or Numbers
append(character: any) {
if (this.equation.length >= 10) {
alert("超出长度限制");
this.clear();
return;
}
//首次输入时不为操作符
if (this.equation === "0" && !this.isOperator(character)) {
if (character === ".") {
this.equation += "" + character; //引号的作用将其转换成字符串
this.isDecimalAdded = true;
} else {
this.equation = "" + character;
}
this.isStarted = true;
return;
}
//if NUmber
if (!this.isOperator(character)) {
//防止连续输入.
if (character === "." && this.isDecimalAdded)