2017年9月13日 星期三

Angular的控制器,ng-controller、$scope、ng-model


MVC:



1.Model:



通常存在資料庫



伺服端有可存取與更新模型的程式



更新完成後,發訊息至Controller



2.Controller



Controller或ViewModel



檢視資料是否更新



更新資料模型



data binding



3.View



不同資料不同檢視(使用者和管理者看到的不同)



 



 


<!DOCTYPE html>
<html lang="en" ng-app>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
</head>
<body>
<div ng-controller='mealCtrl'>
<h2 id="main">主餐:{{main}}</h2>
<h2 id="beverage">飲料:{{beverage}}</h2>
<h2 id="price">價格:{{price}}</h2>
<img src="{{pic}}"><br>
<input type="number" ng-model="quantity">
<h2>小計:{{quantity * price | currency}}</h2>
</div>
<script type="text/javascript">
function mealCtrl($scope){
$scope.main='漢堡';
$scope.beverage='可樂';
$scope.price=100;
$scope.pic="https://api.fnkr.net/testimg/100x100/a00/FFF/?text=A";
$scope.quantity=1;
}
</script>
</body>
</html>




沒有留言:

張貼留言