加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular指令实例及总结

发布时间:2020-12-17 09:31:49 所属栏目:安全 来源:网络整理
导读:html代码 div ng-app = "DnApp" ng-controller = "SeasonDetailController" seamonthdetail seamon = "seasonMonth" getseadatas = "getSeasonDetailData(month)" / seamonthdetail / div controller代码 var DnApp=angular.module( "DnApp" ,[]);DnApp.cont

html代码

<div ng-app="DnApp" ng-controller="SeasonDetailController">
<seamonthdetail seamon="seasonMonth" getseadatas="getSeasonDetailData(month)"></seamonthdetail>
</div>

controller代码

var DnApp=angular.module("DnApp",[]);
DnApp.controller('SeasonDetailController',function($scope){
    $scope.seasonMonth=new Date().getMonth()+1;
    $scope.getSeasonDetailData=function(month){
        console.log(month);
    }  
})

directive代码

DnApp.directive('seamonthdetail',function(){
    return {
        restrict:"EA",replace:true,transclude:true,templateUrl:"/src/tpls/season/monthdetail.html",scope:{
            seamon:"=",/*使用本地别名 seasonmonths:"=seamon" */
            getseadatas:"&"
        },link:function(scope,element,attrs){
            scope.isCurrent=function(name,value){
                if(name==value){
                    return true;
                }else{
                    return false;
                }
            }
            scope.getseasondatas=function(month){
                scope.seamon=month;
                /*调用别名,双向绑定失效 scope.seasonmonths=month */
/*一定记着,如果函数存在形参,那调用时,一定用对象的形式传递实参*/
                scope.getseadatas({month:month});
            }

        }

    }
})

seasondetail.html代码

<div class="flexBox"> <span ng-class="{true:'active'}[isCurrent(seamon,x.month)]" ng-click="getseasondatas(2)" > <i>{{2}}</i> </span> </div> /*为了突出函数调用的注意传参的情况,再举个例子。注意这里的实参传递形式*/ <div class="flexBox"> <span ng-class="{true:'active'}[isCurrent(seamon,x.month)]" ng-click="getseadatas({month:2})" > <i>{{2}}</i> </span> </div>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读