AngularJs學習筆記(一)----------關於資料繫結

來源:互聯網
上載者:User

標籤:view   html   ext   angular   ng-model   scope   model   價格   函數返回   

<!DOCTYPE html5><html>    <head>        <title>AngularJs的練習</title>        <meta charset="UTF-8"/>        <link rel="stylesheet" href="css/3.css"/>    </head>    <body ng-app="myModule1">        <h1>AngularJs中關於資料繫結</h1>        <h3>初識雙向資料繫結</h3>            <label for="uname">單價:</label><input type="text" id="uname" ng-model="price"/>            <label for="num">數量:</label><input type="text" id="num" ng-model="count"/>            <span>小計:{{ price*count }}</span>        <hr/>        <h3>$watch的練習</h3>        <div ng-controller="myCtrl1">            <label for="uname">單價:</label><input type="text" id="uname" ng-model="price"/>            <label for="num">數量:</label><input type="text" id="num" ng-model="count"/>            <span>小計:{{ sum }}</span><!--此處的sum是mode->view的單向綁定,故需要$watch監聽完成所需功能-->        </div>            <script src="js/angular.js"></script>        <script src="js/3.js"></script>    </body></html>

對應的Js:

angular.module(‘myModule1‘,[])       .controller(‘myCtrl1‘,function($scope){               $scope.price=0;               $scope.count=0;               $scope.sum=$scope.price*$scope.count;               // $watch函數返回一個登出監聽的函數               $scope.$watch(‘price‘,function(newVal,oldVal){                   $scope.sum=newVal*$scope.count;//當價格發生變化時,更新sum               });               $scope.$watch(‘count‘,function(newVal,oldVal){                   $scope.sum=newVal*$scope.price;               });       });

 

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.