<body>
<div my-hello></div>
</body>
<script type="text/javascript">
var m1 = angular.module('myApp',[]);
m1.directive('myHello',function(){
return {
restrict : 'A',
replace : true,
template : '<div>hello angular</div>'
};
});
</script>
<div ng-controller="Aaa">
<div my-tab my-id="div1" my-name="name" my-fn="show(num)" class="J-tab"></div>
<div my-tab my-id="div2" my-name="name" my-fn="show(num)" class="J-tab"></div>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',[]);
m1.controller('Aaa',['$scope',function($scope){
$scope.name = 'xiecg';
$scope.age = 18;
$scope.show = function(num){
console.log(num);
};
}]);
m1.directive('myTab',function(){
return {
restrict : 'ECMA',
replace : true, //替换的方式插入内容//绑定策略
scope : {
myId : '@', //解析普通字符串
myName : '=', //解析数据
myFn : '&' //函数
},
controller : ['$scope',function($scope){
//共享数据存放在这里
$scope.name = 'this is a xiecg';
}],
template : '<div id="{{myId}}">\
<input type="button" value="1" class="active" ng-click="myFn({num:456})">\
<input type="button" value="2">\
<input type="button" value="3">\
<div style="display:block;">{{myName}}</div>\
<div>2222</div>\
<div>3333</div>\
</div>'
};
});
</script>
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tab选项卡实例</title>
<style type="text/css">
.J-tab .active{background-color:#03A9F4;}
.J-tab div{display:none;}
</style>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>
<div ng-controller="Aaa">
<my-tab my-id="div1" my-data="sports" class="J-tab"></my-tab>
<my-tab my-id="div2" my-data="time" class="J-tab"></my-tab>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',[]);
m1.controller('Aaa',['$scope',function($scope){
$scope.sports = [
{title : '篮球',content : '111111111'},
{title : '足球',content : '222222222'},
{title : '排球',content : '333333333'}
];
$scope.time = [
{title : '上午',content : '444444444'},
{title : '中午',content : '555555555'}
];
}]);
m1.directive('myTab',function(){
return {
restrict : 'E',
replace : true,
scope : {
myId : '@',
myData : '='
},
controller : ['$scope',function($scope){
$scope.name = 'this is a xiecg';
}],
template : '<div id="{{myId}}">\
<input ng-repeat="data in myData" type="button" ng-value="data.title" ng-class="{active:$first}">\
<div ng-repeat="data in myData" ng-style="{display:$first?\'block\':\'none\'}">{{data.content}}</div>\
</div>',
link : function(scope,element,attr){
element.on('click','input',function(){
var self = $(this) , i = self.index();
self.addClass('active').siblings('input').removeClass('active');
self.siblings('div').eq(i).show().siblings('div').hide();
});
}
};
});
</script>
</body>
</html>
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>自定义指令间的互相交互</title>
<script type="text/javascript" src="js/angular.min.js"></script>
</head>
<body>
<div>
<hello>
<hi></hi>
</hello>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',[]);
m1.directive('hello',function(){
return {
restrict : 'E',
replace : true,
transclude : true, //允许自定义指令的嵌套,通过ng-transclude指定嵌套的范围
controller : function($scope){
$scope.name = 'xiecg';
this.name = 'xiecg'; //使用this共享给其他指令
},
template : '<div>hello angular <h1 ng-transclude></h1></div>'
};
});
m1.directive('hi',function(){
return {
restrict : 'E',
replace : true,
require : '^hello',//hello指令属性hi指令的父级,需要用^符号指定。如果无法指定,使用?容错处理。
template : '<span>hi angular {{name}}</span>',
link : function(scope,element,attr,reController){
console.log(reController); //得到父级hello指令中共享出来的数据
}
};
});
</script>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有