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

相关文章
相关标签/搜索