標籤:
<!DOCTYPE HTML><html ng-app> //ng-app是初始化指令,整個頁面都會被angularjs解析,寫在div或者其他標籤上表示只是局部的div和標籤裡面使用angularjs解析,其餘的不用anguarjs解析。<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script>function Aaa($scope,$timeout){ $scope.name = ‘hello‘; setTimeout(function(){ $scope.name = ‘hi‘;//setTimeout這個定時器不能重新整理name的值 },2000); $timeout(function(){ $scope.name = ‘hi‘;//$timeout是angularjs的定時器才能使name值重新整理 },2000); $scope.show = function(){ $scope.name = ‘hi‘; }; }</script></head><body>//ng-controller是控制器<div ng-controller="Aaa" ng-click="name=‘hi‘"> click函數改變name的值<div ng-controller="Aaa" ng-click="show()"> 作用同上 <p>{{name}}</p></div></body></html>
<!DOCTYPE HTML><html ng-app><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script>function Aaa($scope,$timeout){ $scope.name = ‘hello‘;}</script></head><body><div ng-controller="Aaa"> <input type="text" ng-model="name"> 輸入框改變標籤p也改變。 <p>{{name}}</p></div></body></html>
<!DOCTYPE HTML><html ng-app><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script>function Aaa($scope,$timeout){ $scope.iphone = { money : 5, num : 1, fre : 10 }; $scope.sum = function(){ return $scope.iphone.money * $scope.iphone.num; }; /*$scope.$watch(‘iphone.money‘,function(newVal,oldVal){ console.log(newVal); console.log(oldVal); },true);*/ $scope.$watch($scope.sum,function(newVal,oldVal){ //$watch監聽資料的變化 //console.log(newVal); //console.log(oldVal); $scope.iphone.fre = newVal >= 100 ? 0 : 10; });}</script></head><body><div ng-controller="Aaa"> <p>價格:<input type="text" ng-model="iphone.money"></p> <p>個數:<input type="text" ng-model="iphone.num"></p> <p>費用:<span>{{ sum() | currency:‘¥‘ }}</span></p> currency是過濾器 <p>運費:<span>{{iphone.fre | currency:‘¥‘}}</span></p> <p>總額:<span>{{ sum() + iphone.fre | currency:‘¥‘}}</span></p></div></body></html>
angularjs ng-app