AngularJS第一個小例子

<!DOCTYPE html>   
<html ng-app>   
<head>   
<meta charset="utf-8">   
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">   
<title>Examples</title>   
<meta name="description" content="">   
<meta name="keywords" content="">   
<link href="" rel="stylesheet">   
<style type="text/css">   
    span{display: inline-block;min-width: 100px;}   
    input{width: 100px;}   
</style>   
</head>   
<body ng-Controller="demoController">   
    <h1>{{demotitle}}</h1>   
    <div>   
        <span>標題</span>   
        <span>數量</span>   
        <span>單價</span>   
        <span>總結</span>   
        <span>操做</span>   
    </div>   
    <div ng-repeat="item in items">   
        <span>{{item.title}}</span>   
        <input type="text" ng-model="item.quantity"/>   
        <span>{{item.price | currency}}</span>   
        <span>{{item.price * item.quantity | currency}}</span>   
        <button ng-click="remove($index)">remove</button>   
    </div>   
<script src="angular.min.js"></script>   
<script>   
function demoController($scope){   
    $scope.demotitle = "第一個小例子";   
    $scope.items = [   
        {title:'test1',quantity:0,price:2.2},   
        {title:'test2',quantity:1,price:1.5},   
        {title:'test3',quantity:0,price:3.3},   
        {title:'test4',quantity:2,price:4}   
    ];   
    $scope.remove = function(index){   
        $scope.items.splice(index,1);   
    }   

}   
</script>   
</body>   
</html>

生成的頁面以下。
請輸入圖片描述
這是《用AngularJS開發下一代Web應用》這本書裏的一個例子,用來理解AngularJS的一些特性,MVC,其中輸入框爲Model,定義了ng-model後用來在輸入框和item.quantity的值之間建立數據綁定關係,而{{}}這樣的結構用來展現咱們在controller中定義的值。css

ng-repeat的意思是講items的做用,不知道怎麼說好,主要是用來循環提取items數組中每一個屬性的值而後添加到dom中。html

button控件綁定的click事件用來移除當前行,ng-click在controller中定義,傳入index的值用來達到移除的效果。chrome

前邊這段話寫的亂七八糟的。僅看成一個記錄吧。segmentfault

相關文章
相關標籤/搜索