AngularJS學習——價格計算機

來源:互聯網
上載者:User

標籤:

利用AngularJs實現價格計算機,總價滿100免運費。(熟悉$watch的使用)

代碼:
<!DOCTYPE html><html lang="en" ng-app="myApp"><head>    <meta charset="UTF-8">    <title>價格計算機</title>    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css"></head><body>    <div class="container" ng-controller="MyControl">        <h2>價格計算機</h2>        單價:<input type="text" class="form-control" ng-model="iphone.price">        數量:<input type="text" class="form-control" ng-model="iphone.num">        <hr>        <p>總價:{{ sum() | currency:‘¥‘}}</p>        <p>運費:{{ iphone.yf | currency:‘¥‘}}</p>        <p>合計:{{ sum() + iphone.yf | currency:‘¥‘}}</p>    </div>    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>    <script>        var myAng = angular.module(‘myApp‘,[]);        myAng.controller(‘MyControl‘,function($scope){            $scope.iphone = {                price:10,                num:1,                yf:10            }            $scope.sum = function(){                return $scope.iphone.price*$scope.iphone.num            }            // $watch監控對象為屬性時,不加$scope            $scope.$watch(‘iphone.num‘,function(newvalue,oldvalue){                console.log(‘新值:‘+ newvalue +‘;舊值:‘+ oldvalue)            })            // $watch監控對象為方法時,加$scope            $scope.$watch($scope.sum,function(newvalue,oldvalue){                return $scope.iphone.yf = newvalue >= 100 ? 0 : 10;            })        })    </script></body></html>
 

 

 

AngularJS學習——價格計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.