AngularJS1.x学习(directive 中‘& ’‘=’ ‘@’符号的区别使用)

来源:互联网 发布:asp.net 高级编程 编辑:程序博客网 时间:2024/05/08 16:54

这里写图片描述

对于HTML5的兴起,Angular火热绝对值得称赞,外加庞大的社区做支持,是的Angular的普及度越来越高,称为最为流行的html5框架。

对于一个html5框架的好坏,我们有几个评判标准, 轻量级,可拓展,易复用,速度快。

对组件复用这点,angular以directive的形式展示给开发者,是一个还算不错的选择,作为一个UI组件,必定存在数据交互。

那么数据交互过程中的几个符号我们一定要有所了解,以及他们的区别是什么,防止我们在运用过程中出错。

1. 首先,我们看一scope作用域下面@的使用:

html

<!doctype html>  <html ng-app='myApp'>    <head>      </head>    <body>        <div ng-controller="listCtrl">         <input type="text"  ng-model="t" />       <test title="{{t}}" >          <span>我的angularjs</span>      </test>  </div>   <script type="text/javascript" src="angular.js"></script>  <script type="text/javascript" src="main.js"></script>  </body></html>  

js

var myApp=angular.module('myApp',[]);  myApp.controller('listCtrl',function($scope){     $scope.logchore="motorola";  });  myApp.directive('test',function(){      return {          'restrict':'E',          scope:{              title:"@"          },          template:'<div >{{title}}</div>'      }  });  

这个必须指定的,这里的title是指令里scope的@对应的,t就是控制域scope下的 .

2. = 的使用。

html

<!doctype html>  <html ng-app='myApp'>    <head>      </head>    <body>        <div ng-controller="listCtrl">         <input type="text"  ng-model="t" />       <test title="t" >         <p>{{title}}</p>          <span>我的angularjs</span>      </test>  </div>   <script type="text/javascript" src="angular.js"></script>  <script type="text/javascript" src="main05.js"></script>  </body></html>  

js

var myApp=angular.module('myApp',[]);  myApp.controller('listCtrl',function($scope){     $scope.logchore="motorola";  });  myApp.directive('test',function(){      return {          'restrict':'E',          scope:{              title:"="          },          template:'<div >{{title}}</div>'      }  });  

和上面@相比,这个直接赋值等于scope域下的t了

3. 最好我们看看&符号的使用

html

<!doctype html>  <html ng-app='myApp'>    <head>      </head>    <body>        <div ng-controller="listCtrl">           <test flavor="logchore()" ></test> </div>   <script type="text/javascript" src="angular.js"></script>  <script type="text/javascript" src="main05.js"></script>  </body></html>  

js

var myApp=angular.module('myApp',[]);  myApp.controller('listCtrl',function($scope){     $scope.logchore=function(){          alert('ok');     };  });  myApp.directive('test',function(){      return {          'restrict':'E',          scope:{              flavor:"&"            },          template:'<div ><button ng-click="flavor()"></button></div>'      }  });  

尝试一下,就明白了,简洁明了!

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 拉链一边掉了怎么办 拉链一半掉了怎么办 去北戴河怎么办安检证 电脑空格键打字失灵怎么办 国外玩游戏延迟怎么办 韩服lol要求输韩文怎么办 智齿碰到牙神经怎么办 合影中有人去世怎么办 在家中遇到蛇怎么办 去澳门手机充电怎么办 商标撕下来胶怎么办 商标被提出异议怎么办 商标被别人用怎么办 商标申请不下来怎么办 注册商标有近似商标怎么办 电话话打不通怎么办 公司卖了商标怎么办 商标撕不下来怎么办 商标撕不下来时怎么办 商标还没下来怎么办 商标揭不下来怎么办 手机程序出现异常怎么办 公众号企业名称是*怎么办 家人生命受到威胁怎么办 海淘看不懂英文怎么办 对英语不感兴趣怎么办 装修无合同起诉怎么办 上海离职后档案怎么办 公司注销了银行帐户怎么办 360借条注销了怎么办 注销营业执照公章丢失怎么办 工厂招聘信息有假怎么办 个体餐饮怎么办核名 见父母后接下来怎么办 工商核名重名怎么办 核名通知书过期怎么办 包头鼎太风华怎么办 用人单位不续签劳动合同怎么办 全是英文看不懂怎么办 孩子智力发育晚怎么办 公司比赛成绩不好怎么办