angularjs学习系(1)

讲述angularjs创建指令replace,transclude域的用法


angularjs的指令创建有四种形式,比如创建的指令hello:

(1)页面标签的形式E:

               <hello>
  <div>angularjs创建指令</div>
  <div>replace的用法</div>
</hello> 

(2)标签属性的形式A(angularjs默认的):<div hello></div>

(3)标签样式类的形式C:<div class="hello"></div>

(4)页面注释的形式M: <!-- directive:hello -->(注释号前后有个空格)


指令的创建代码:

myApp.directive('hello',function(){
	return {
		restrict:'ACEM',
		template:'<div>hello everyone!</div>',
		//transclude:false
		replace:false
	}
});
【一】如果replace设置为false,最终页面展现的html结果如下:

      C: <div class="hello"><div>hello everyone!</div></div>
      A: <div hello=""><div>hello everyone!</div></div>
      E: <hello><div>hello everyone!</div></hello> 
      M: <!-- directive:hello -->(没有效果,无效的)
【二】如果replace设置为true,最终页面展现的html结果如下:
      C:<div class="hello">hello everyone!</div>
      A:<div hello="">hello everyone!</div>
      E:<div>hello everyone!</div> 
      M:<div hello="">hello everyone!</div> (有效)


使用replace有个什么缺点呢,比如我要把文章开头指令hello下的 <div>angularjs创建指令</div> <div>replace的用法</div>要显示在页面上,replace是办不到的,这个可以用transclude域的设置来解决:

myApp.directive('hello',function(){
	return {
		restrict:'ACEM',
		template:'<div>hello everyone!<span ng-transclude></span></div>',
		transclude:true
	}
});
这样就可以把指令内的DOM标签展现出来:

       C:<div class="hello"><div>hello everyone!<span ng-transclude=""><span class="ng-scope">样式类</span></span></div></div>
       A:<div hello=""><div>hello everyone!<span ng-transclude=""><span class="ng-scope">属性</span></span></div></div>
       E:<hello><div>hello everyone!<span ng-transclude="">
		   <div class="ng-scope">angularjs创建指令</div>
		   <div class="ng-scope">replace的用法</div>
		</span></div></hello> 
       M:<!-- directive:hello -->




参考:http://stackoverflow.com/questions/15285635/how-to-use-replace-of-directive-definition

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值