標籤: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學習筆記(一)----------關於資料繫結