左加右减 总和

<head>
    <meta charset="UTF-8">
    <title></title>
    <!-- 安装 -->
    <script src="js/angular.min.js"></script>
    <script>
        var app = angular.module("gaoyn", []); //创建模块,angular以模块的形式来组织
        //2 通过模块来创建控制器
        app.controller("democ1", function($scope) { //$scope:视图(body)和控制器之间的纽带(数据和方法)
            $scope.stus = [{
                name: '张三',
                sex: '男',
                sal: 10000,
                num:1
            }, {
                name: '李四',
                sex: '男',
                sal: 18000,
                num:2
            }];


            $scope.add=function(){
                    $scope.stus.push({
                    name: '李小三',
                    sex: '男',
                    sal: 10,
                    num:1
                });
            }

            $scope.count=function(n,i){
                $scope.stus[i].num=$scope.stus[i].num+n;
                if($scope.stus[i].num==0){
                    $scope.stus.splice(i,1);
                }
            }

            //计算所有人工资的方法;在控制器中定义方法
            $scope.sum = function() {
                var count = 0;
                for(var i in $scope.stus) {
                    count = count + $scope.stus[i].sal*$scope.stus[i].num;
                }
                return count;
            }

        })
    </script>

    <style>
        tr button{
            width: 30px;
        }
    </style>
</head>

<body ng-app="gaoyn" ng-controller="democ1">

    <!-- ng-click调用的是控制器的方法  onclick JavaScript原始的定义方法 -->
    <button ng-click="add()">添加学生</button>

    <!--ng-modle  用在form表单的类型标签,把value的值自动赋值给stu   -->
    <table border="1px">
        <tr>
            <td>序号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>工资</td>
            <td>级别</td>
            <td>小计</td>
        </tr>
        <tr ng-repeat="s in stus ">
            <td>{{$index}}</td>
            <td>{{s.name}}</td>
            <td>{{s.sex}}</td>
            <td>{{s.sal}}</td>
            <td><button ng-click="count(+1,$index)">+</button> <input style="width: 30px;" ng-model="s.num" /><button ng-click="count(-1,$index)">-</button> </td>
            <td>{{s.num*s.sal}}</td>
        </tr>
    </table>
    总记:{{sum()}}
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值