angular指令实例及总结

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>

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...