<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularDirective</title>
<script src="http://cdn.bootcss.com/angular.js/1.4.6/angular.js"></script>
</head>
  <body ng-app="angularJS" >
    <!-- <div class="self-direct">{{title}}<input type="text" ng-model='title'></div> -->

    <!-- <input type="text" ng-model="color">
      <self-direct color='{{color}}'></self-direct>
      <self-direct m-color='{{color}}'></self-direct> -->

    <!-- <input type="text" ng-model="color">
      <self-direct color='color'></self-direct>如果采用双向绑定,指令中的属性值默认是变量,所以不用添加{{}}
      <self-direct m-color='color'></self-direct> -->

    <!-- <self-direct logo='logo()'></self-direct> -->

    <!-- <self-direct ></self-direct> -->

    <!-- <self-direct ></self-direct> -->

    <self-direct title="JinDong" bgcolor="red" fontcolor="#fff"></self-direct>
    <script type="text/javascript">

  /*Angular.js自定义指令的格式和相关参数与其值:
  let m=angular.module('angularJS',[]);
  m.directive('selfDirect',[function(){//selfDirect表示自定义指令的名字,采用驼峰命名法,当restrict的值为E的时候:<self-direct></self-direct>
    return {
      restrict:'A/E/C',//A:Attrabute,E:Elements,C:class;restrict属性表示生成指令在页面中的表现形式,字母必须大写,不建议使用C,因为C的写法与CSS耦合性太强.
      template:'<p>template选项表示指令在页面中显示的内容,template的值可以是字符串也可以是HTML的标签形式,也可以为函数,如:template:function(elle,attr){return '<span style="'color:'+attr['color']+'">'+ele.html()+'</span>'},view内容太多的时候不建议使用函数的形式</p>',
      replace:true,//使用模板内容替换包含模板内容的父级标签
      transclude:true,//其内容填充到ng-transclude指定的位置
      templateUrl:'',//不可与template同时使用
      scope:true,//默认为false,设置指令的作用域,当值为{}时,模板中的变量不会继承来自控制器中的属性值,
      controller:['$scope',function($scope){$scope.data={...}}],//指令中的控制器
      link:function(scope,elem,attr){},//用link完成对DOM的操作,scope:指令的作用域,elem:指令标签元素,attr:指令标签元素的属性数组,
    };
  }])*/

  var m=angular.module('angularJS',[]);
  m.directive('selfDirect', [function () {
    return {
      restrict: 'E',
      //template:'<h1><span ng-transclude=""></span>This is a Angular.js direction of self definition</h1><div ng-transclude=""></div>',
      //replace:true,
      //transclude:true,
      //templateUrl:'viewModel.html',
      //scope:{},
      //template:'{{title}}<input type="text" ng-model="title">',
      //template:'<p style="color:{{color}}">suNing store</p><input ng-model="color">',
      //scope:{color:'@mColor'},//控制器和指令隔离作用域@单项文本绑定,控制器可以影响指令中的数据,而指令不能影响控制器中的数据
      //scope:{color:'=mColor'},//控制器和指令隔离作用域=双向文本绑定,控制器可以影响指令中的数据,指令也可以影响控制器中的data
      //template:'<p>{{logo()}}</p>',
      //scope:{logo:'&'},//用&符号调用父控制器中的方法
      /*replace:true,
      templateUrl:'viewModel.html',
      controller:['$scope',function($scope){
        $scope.data=[{
          id:1,title:'puDong'
        },{
          id:2,title:'JinDong'
        },{
          id:3,title:'TianMao'
        }];
      }],*/
      scope:{title:'@'},
      link:function(scope,elem,attr){
        $(elem).css({backgroundColor:attr['bgcolor'],color:attr['fontcolor']}).html(scope.title);
        },
      };
      }]);
    /*m.controller('ctrl',['$scope',function($scope){
      $scope.title='SuNing store';
      $scope.color='red';
      $scope.logo=function(){
      return 'TianMao store';
     };
    }]);*/
</script>
</body>
</html>

Angular.js之自定义指令学习笔记的更多相关文章

  1. Angular.js之内置过滤器学习笔记

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. angular.js创建自定义指令-demo3

    html: <!doctype html><html ng-app="myModule"> <head> <meta charset=&q ...

  3. 推荐 15 个 Angular.js 应用扩展指令(参考应用)

    几天前我们看到Angular 1.4.0发布了-一个以社团为驱动的发布版本涵盖了400多个GitHub的提交,增加了对特性的提升,比如动画,以及可用性. 官方新闻发布稿 覆盖了绝大部分,这同样值得放于 ...

  4. 《JS高程》事件学习笔记

    事件:文档或浏览器窗口中发生的一些特定的交互瞬间,也即用户或浏览器自身执行的某种动作. -------------------------------------------------------- ...

  5. Vue.js:自定义指令

    ylbtech-Vue.js:自定义指令 1.返回顶部 1. Vue.js 自定义指令 除了默认设置的核心指令( v-model 和 v-show ), Vue 也允许注册自定义指令. 下面我们注册一 ...

  6. Angular.js之指令学习笔记

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  7. Angular.js之服务与自定义服务学习笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Angularjs之directive指令学习笔记(二)

    1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...

  9. angular这个大梗的学习笔记

    angular定义一个模块(module)及控制器(controller)的局部声明方法: var app=angular.module("Myapp",[]); myapp.co ...

随机推荐

  1. javascript--hasOwnProperty()+isPrototypeof()+in方法

    1.hasOwnProperty():用来检测对象中是否包含给出的函数属性或者对象,但是无法检测出对象的原型链中是否包含给出的属性或对象--该属性或者对象必须在函数内部被声明 2.isPrototyp ...

  2. Bzoj3756

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3756 题解:乱搞 代码: #include<iostream> #include ...

  3. 关于String的相关常见方法

    package Stirng类; /** * String 常见的相关方法摘要 * @author Administrator * */ public class DemoStringMethod { ...

  4. 三 APPIUM Android自动化 测试初体验

    1.创建一个maven项目 成功新建工程: 编辑pom.xml,在<dependencies></dependencies>下添加appium相关依赖: <depende ...

  5. 读【10问PHP程序员】 有感

    http://bbs.phpchina.com/thread-174331-1-1.html 看到前人的文章,总结自己的学习心得,颇有感悟,下面是自己的总结,平时就拿出来多问问自己.1.上了十几年的学 ...

  6. animate的{queue:false,duration:400}意思

    示例:$(document).ready(function(){ $('.tmplS').hover(function(){ $(".cover", this).stop().an ...

  7. eclipse调试找不到源解决办法

    eclipse调试时有时显示找不到源码,首先得确定代码没问题 这是eclipse没有发现工程源码,解决办法是 右键工程>>Debug As >> Debug configura ...

  8. pureMVC简单示例及其原理讲解四(Controller层)

    本节将讲述pureMVC示例中的Controller层. Controller层有以下文件组成: AddUserCommand.as DeleteUserCommand.as ModelPrepCom ...

  9. bootstrap 基础表单 内联表单 横向表单

    bootstrap 基础表单 内联表单 横向表单 <!DOCTYPE html> <html> <head> <title></title> ...

  10. Redis缓存数据之简单逻辑

    并发不高的情况: 读: 读redis->没有,读DataBase->把DB数据写回redis,有的话直接从redis中取: 写: 写DataBase->成功,再写redis: 并发高 ...