AngularJs模块化指令03(点赞)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模块化应用03(点赞)</title>
    <script src="angular.js"></script>
    <style>
        li{
            padding: 18px 8px;
        }
    </style>
    <script>
        var app=angular.module("modelDemo03",[]);
        app.controller("modelCtrl03",function($scope){
            $scope.UserTitleList=[
                {UserName:'Tom',Title:'今天天气很好的样子',Time:'2016-05-12',LikeAmount:0,LikeList:[]},
                {UserName:'Jerry',Title:'我们出去玩好么',Time:'2016-05-12',LikeAmount:0,LikeList:[]},
                {UserName:'Jack',Title:'算了,宝宝要敲代码。。。',Time:'2016-05-12',LikeAmount:0,LikeList:[]}
            ];
        });

        app.directive("like",function(){
            var direction={};
            direction.restrict='AE';
            direction.template="<button ng-click='Like()'>赞</button>";

            //这是一个对象
            //意思是我们将循环中的userTitle的这个对象传给了指令的content,而我们在指令content里面就可以对content对象改变它的likeAmount的值,
            //而这个LikeAmount的值会反馈到我们的controller里面,而controller对应的UserTitleList当中每一个被改变过的LikeAmount的值都会反应到
            //ng-repeat循环的元素里面去
            direction.scope={
                content:"="//这是对象里的属性,是我们自己定义的
            };

            //link指令
            direction.link=function($scope,element){
                $scope.Like=function(){
                    $scope.content.LikeAmount=$scope.content.LikeAmount+1;
                    $scope.content.LikeList.push({Text:"jerry点了赞"});
                }
            };

            return direction;
        });
    </script>
</head>
<body ng-app="modelDemo03" ng-controller="modelCtrl03">
    <!--同一个人对同一条数据进行不断的点赞-->
    <ul>
        <li ng-repeat="userTitle in UserTitleList">
            {{userTitle.UserName}}<br/>
            <h4>{{userTitle.Title}}</h4><span>{{userTitle.LikeAmount}}</span>
            <like content="userTitle"></like><br/>
            {{userTitle.Time}}
            <ul>
                <li ng-repeat="likeList in userTitle.LikeList">
                    <span>{{likeList.Text}}</span>
                </li>
            </ul>
        </li>
    </ul>
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值