从现有javascript代码创建angularjs应用程序

从现有javascript代码创建angularjs应用程序,javascript,angularjs,Javascript,Angularjs,我已经构建了一个简单的税务计算器,我想将它集成到angularjs应用程序中。我已经看过angularjs了,但还不能完全理解如何集成现有的js代码。有人能推荐一种简单的方法(重新)将其构建到angularjs应用程序中吗 谢谢 //constructor to create the initial object. Accepts object and creates new taxCalculation object function taxCalculation(configuration)

我已经构建了一个简单的税务计算器,我想将它集成到angularjs应用程序中。我已经看过angularjs了,但还不能完全理解如何集成现有的js代码。有人能推荐一种简单的方法(重新)将其构建到angularjs应用程序中吗

谢谢

//constructor to create the initial object. Accepts object and creates new taxCalculation object
function taxCalculation(configuration){
      this.superAnnuationPercentage = configuration.superAnnuationPercentage;
      this.superAnnuationTaxRate    = configuration.superAnnuationTaxRate;
};

//creating a new object "tax" with taxCalculation constructor

var tax = new taxCalculation({
    superAnnuationPercentage: 9.25,
    superAnnuationTaxRate:  15
});

//Defines HECS/HELP repayment brackets
var hecs = [
{from:0,     percentage:0},
{from:53346, percentage:4},
{from:59422, percentage:4.5},
{from:65498, percentage:5.0},
{from:68940, percentage:5.5},
{from:74106, percentage:6.0},
{from:80258, percentage:6.5},
{from:84482, percentage:7.0},
{from:92971, percentage:7.5}, 
{from:99070, percentage:8.0},
];

//Defines the tax brackets
var taxBracket = [
    {from: 0, percentage: 0, baseAmount: 0},
    {from: 18201, percentage: 19, over: 18200, baseAmount: 0},
    {from: 37001, percentage: 32.5, over: 37000, baseAmount: 3752},
    {from: 80001, percentage: 37, over: 80000, baseAmount: 17547},
    {from: 180001, percentage: 45, over: 180000, baseAmount: 54547}
];

//Calculate the super annuation component of grossIncome
taxCalculation.prototype.grossSuperAnnuation = function(income){
    return income * this.superAnnuationPercentage / 100;

};



//Calculates tax on super annuation component
taxCalculation.prototype.superAnnuationTax = function(grossSuper){
  return grossSuper * this.superAnnuationTaxRate / 100;
};


//Calculate gross income
taxCalculation.prototype.grossIncome = function(grossIncome){
    return grossIncome - this.grossSuperAnnuation(grossIncome);

};


//Calculates HECS bracket based on gross income
taxCalculation.prototype.hecsCalculation = function (income){
        for (var x = 0; x < hecs.length; x++){
            if(income >= hecs[x].from){
            var percentage = hecs[x].percentage/100;
            }
        };        
        return income * percentage;
};


//Loops through taxBracket object and finds the tax bracket for the post deduction gross income amount
taxCalculation.prototype.taxBracketCalculation = function (grossSuperIncome){
    for(var x = 0; x < taxBracket.length; x++){
        if(grossSuperIncome <= taxBracket[x].from){
            var amountOver = grossSuperIncome - taxBracket[x].over;
            var percent = taxBracket[x].percentage / 100;
            return taxBracket[x].baseAmount + (amountOver * percent);
        }

    };
};

//Calculate all of the factors in tax calculation
taxCalculation.prototype.totalTax = function (income){
  var taxResult = {
    income: income
  };
  taxResult.superAnnuation = this.grossSuperAnnuation(income);
  taxResult.grossIncome = this.grossIncome(income);
  taxResult.taxBase = this.taxBracketCalculation(taxResult.grossIncome);
  taxResult.taxSuper = this.superAnnuationTax(taxResult.superAnnuation);
  taxResult.hecs = this.hecsCalculation(income);

  return taxResult;
};

var test = tax.totalTax(65000);
console.log(test);
//用于创建初始对象的构造函数。接受对象并创建新的taxCalculation对象
功能计算(配置){
this.退休金百分比=配置.退休金百分比;
this.superAnnuationTaxRate=配置.superAnnuationTaxRate;
};
//使用taxCalculation构造函数创建新对象“tax”
var税=新的税收计算({
养老金比例:9.25,
退休金税率:15
});
//定义HECS/帮助还款括号
var hecs=[
{from:0,百分比:0},
{来自:53346,百分比:4},
{起始:59422,百分比:4.5},
{from:65498,百分比:5.0},
{起始:68940,百分比:5.5},
{from:74106,百分比:6.0},
{从:80258,百分比:6.5},
{from:84482,百分比:7.0},
{from:92971,百分比:7.5},
{from:99070,百分比:8.0},
];
//定义税级
var Tax括号=[
{from:0,percentage:0,baseAmount:0},
{起始:18201,百分比:19,超过:18200,基数:0},
{起始:37001,百分比:32.5,超过:37000,基数:3752},
{起始:80001,百分比:37,超过:80000,基数:17547},
{起始:180001,百分比:45,超过:180000,基数:54547}
];
//计算Grossincom的超级年金部分
taxCalculation.prototype.GrossPerannuation=函数(收入){
返回收入*this.退休金百分比/100;
};
//计算超级年金部分的税收
taxCalculation.prototype.退休金税=函数(Grossuper){
return grossuper*this.退休金税率/100;
};
//计算总收入
taxCalculation.prototype.Grossincom=函数(Grossincom){
return grossIncome-这个.grossuperannuation(grossIncome);
};
//根据总收入计算HECS等级
taxCalculation.prototype.hecsCalculation=函数(收入){
对于(变量x=0;x=hecs[x]。来源){
var百分比=hecs[x]。百分比/100;
}
};        
收益*百分比;
};
//循环遍历taxBracket对象并查找扣除后总收入金额的税级
taxCalculation.prototype.taxBracketCalculation=函数(GrossUserCome){
对于(var x=0;x如果(grossuperincom正如Shomz和Huafu已经解释的那样,您应该将代码放入angularjs服务中

创建一个包含所有代码的服务。但我认为您可能必须更改函数的名称。例如,只需更改代码的一个子集:

app.service('taxService', function () {

function taxCalculation(configuration) {
    this.superAnnuationPercentage = configuration.superAnnuationPercentage;
    this.superAnnuationTaxRate = configuration.superAnnuationTaxRate;
};

//creating a new object "tax" with taxCalculation constructor
var tax = new taxCalculation({
    superAnnuationPercentage: 9.25,
    superAnnuationTaxRate: 15
});


//Calculate the super annuation component of grossIncome
this.grossSuperAnnuation = function (income) {
    return income * tax.superAnnuationPercentage / 100;

};


}
顺便说一句,我认为您的代码中有一个输入错误,
this.superAnnuationPercentage
在对象
tax
之外不存在

然后引用控制器内的服务:

app.controller('IncomeController',

function IncomeController($scope, taxService) {
    $scope.tax = "undefined"
    $scope.calculate = function () {
        $scope.tax = taxService.grossSuperAnnuation($scope.income);
    }
});
参考控制器并从angularjs页面调用
计算
函数:

<div ng-controller="IncomeController">
    <input ng-model="income" />
    <button ng-click="calculate()">Calculate</button>
    <p>{{tax}}</p>
</div>

算计
{{tax}


我会把整个东西放在一个服务中。是的,这更像是一个服务,不依赖任何UI。你可以使用Angular、Ember、Backbone,无论框架是什么,都是一样的,你既不存储任何东西,也不显示任何东西(除了控制台中)