angularjs中directive声明scope对象的用法
总的来说用法 分三种:
》1: scope: false --> 继承父域,实现 双向数据绑定
示例代码 可自测:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>directive属性 scope:{}</title>
</head>
<body ng-controller="mainCtrl">
<p>scope:false --> 继承父域,实现 双向数据绑定</p>
姓名:<input type="text" name="" ng-model="myName">
年龄:<input type="text" name="" ng-model="myAge">
性别:<input type="text" name="" ng-model="mySex" > <div my-directive name="myName" this-is-age="myAge" sex="mySex" say-words="say(arg)"></div> <script type="text/javascript" src="../../../../lib/jquery/v1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="../../js/angular-v1.4.6.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
// mainCtrl
myApp.controller('mainCtrl', mainController);
// myDirective
myApp.directive('myDirective', myDirective ) // main controller
function mainController($scope){
$scope.myName = 'jcy';
$scope.myAge = '22';
$scope.mySex = 'male';
$scope.info = '想知道我的个人信息吗,不告诉你。。。';
$scope.say = function(arg){
alert(arg);
}
}; // my directive
function myDirective(){
return {
scope: false, // {} = isolate, true = child, false/undefined = no change
restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
template: '<div>'+
'<button>我要说话</button>'+
'<p>'+
'姓名:<input type="text" name="" ng-model="myName">'+
'年龄:<input type="text" name="" ng-model="myAge">'+
'性别:<input type="text" name="" ng-model="mySex" > <br>'+
'介绍1:<span ng-bind="info"></span><br>'+
'介绍2:<span>{{info}}</span>'+
'</p>'+
'</div>',
link: function($scope, iElm, iAttrs, controller) {
$(iElm).on('click','button',function(e){
var words = '注意这个传值方式哦' ;
$scope.say( { arg:words } );
});
}
};
} </script> </body>
</html>
》2: scope: true -->
初始化,继承父域;
子域属性值没有发生改变前,可实现 单向数据绑定(父变 --> 子变);
子域属性值发生改变后,实现子域与发父域隔离(父变 --> 子不变);
示例代码 可自测:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>directive属性 scope:{}</title>
</head>
<body ng-controller="mainCtrl">
<p>scope:true --> 初始化,继承父域;
子域属性值没有发生改变前,可实现 单向数据绑定(父变 --> 子变);
子域属性值发生改变后,实现子域与发父域隔离(父变 --> 子不变)</p>
姓名:<input type="text" name="" ng-model="myName">
年龄:<input type="text" name="" ng-model="myAge">
性别:<input type="text" name="" ng-model="mySex" > <div my-directive name="myName" this-is-age="myAge" sex="mySex" say-words="say(arg)"></div> <script type="text/javascript" src="../../../../lib/jquery/v1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="../../js/angular-v1.4.6.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
// mainCtrl
myApp.controller('mainCtrl', mainController);
// myDirective
myApp.directive('myDirective', myDirective ) // main controller
function mainController($scope){
$scope.myName = 'jcy';
$scope.myAge = '22';
$scope.mySex = 'male';
$scope.info = '想知道我的个人信息吗,不告诉你。。。';
$scope.say = function(arg){
alert(arg.arg);
}
}; // my directive
function myDirective(){
return {
scope: true, // {} = isolate, true = child, false/undefined = no change
restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
template: '<div>'+
'<button>我要说话</button>'+
'<p>'+
'姓名:<input type="text" name="" ng-model="myName">'+
'年龄:<input type="text" name="" ng-model="myAge">'+
'性别:<input type="text" name="" ng-model="mySex" > <br>'+
'介绍1:<span ng-bind="info"></span><br>'+
'介绍2:<span>{{info}}</span>'+
'</p>'+
'</div>',
link: function($scope, iElm, iAttrs, controller) {
$(iElm).on('click','button',function(e){
var words = '这是要测试 scope绑定函数,并且给函数传值的方式' ;
$scope.say( { arg:words } );
});
}
};
} </script> </body>
</html>
》3:
scope 的绑定方式:“@”、“=”、“&”
绑定的名称:要全为小写 中间可用 “-” 符号连接, 绑定到到 scope中时,去掉“-”,并将“-”后第一个字符改为大写,驼峰式命名
scope 绑定方式的区别:
“=”:指令中的属性取值为controller中对应$scope上属性的取值,可用于双向数据的绑定.
“@”:
1.指令中的取值为html中的字面量/直接量. 即:attr="xxx"时,"@attr"形式得到的是 “xxx”字符串;
2.绑定 controller中的$scope property. 即:arrt="{{xxx}}",或者其它绑定$scope.property时,“@attr"形式得到的是$scope.property。
可用于单向数据绑定。父(改变)-->子(改变),子(改变)-->父(不改变)。
“&”:指令中的取值为Contoller中对应$scope上的属性,但是这属性必须为一个函数回调。
当为"func:&aa"时,传值方式 $scope.func({ arg1:"xxx", arg2:"xxxx", .... }), html如 <div my-directive aa="xxfunction(arg1, arg2,......)"></div>
示例代码 可自测:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>directive属性 scope:{}</title>
</head>
<body ng-controller="mainCtrl">
<p>
总结:<br>
scope 的绑定方式:“@”、“=”、“&” <br>
绑定的名称:要全为小写 中间可用 “-” 符号连接, 绑定到到 scope中时,去掉“-”,并将“-”后第一个字符改为大写,驼峰式命名 <br>
scope 绑定方式的区别:<br>
“=”:指令中的属性取值为controller中对应$scope上属性的取值,可用于双向数据的绑定. <br>
“@”:
1.指令中的取值为html中的字面量/直接量. 即:attr="xxx"时,"@attr"形式得到的是 “xxx”字符串;<br>
2.绑定 controller中的$scope property. 即:arrt="{{xxx}}",或者其它绑定$scope.property时,“@attr"形式得到的是$scope.property。
可用于单向数据绑定。父(改变)-->子(改变),子(改变)-->父(不改变)。<br>
“&”:指令中的取值为Contoller中对应$scope上的属性,但是这属性必须为一个函数回调。
当为"func:&aa"时,传值方式 $scope.func({ arg1:"xxx", arg2:"xxxx", .... }), html如 <pre><div my-directive aa="xxfunction(arg1, arg2,......)"></div></pre> <br>
</p> 姓名:<input type="text" name="" ng-model="myName">
年龄:<input type="text" name="" ng-model="myAge">
性别:<input type="text" name="" ng-model="mySex" > <div my-directive name="myName" this-is-age="myAge" sex="{{mySex}}" say-words="say(arg)"></div> <script type="text/javascript" src="../../../../lib/jquery/v1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="../../js/angular-v1.4.6.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
// mainCtrl
myApp.controller('mainCtrl', mainController);
// myDirective
myApp.directive('myDirective', myDirective ) // main controller
function mainController($scope){
$scope.myName = 'jcy';
$scope.myAge = '22';
$scope.mySex = 'male';
$scope.say = function(arg){
alert(arg);
}
}; // my directive
function myDirective(){
return {
scope: {
name: "=",
age: "=thisIsAge",
sex: "@",
say: "&sayWords"
}, // {} = isolate, true = child, false/undefined = no change
restrict: 'AE', // E = Element, A = Attribute, C = Class, M = Comment
template: '<div>'+
'<button>我要说话</button>'+
'<p>'+
'姓名:<input type="text" name="" ng-model="name">'+
'年龄:<input type="text" name="" ng-model="age">'+
'性别:<input type="text" name="" ng-model="sex" >'+
'</p>'+
'</div>',
link: function($scope, iElm, iAttrs, controller) {
$(iElm).on('click','button',function(e){
var words = '这是要测试 scope绑定函数,并且给函数传值的方式' ;
$scope.say( { arg:words } );
});
}
};
} </script> </body>
</html>
angularjs中directive声明scope对象的用法的更多相关文章
- angularjs于directive声明scope说明何时以及如何使用对象修饰符
于angular我们定义directive方法.查看 return { restrict: 'AE', scope: {}, template: '<div></div>', ...
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- AngularJS中Directive指令系列 - scope属性的使用
文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- angularJS中directive与controller之间的通信
当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...
- AngularJS中Directive指令系列 - 基本用法
参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...
- angularJS中directive与directive 之间的通信
上一篇讲了directive与controller之间的通信:但是我们directive与directive之间的通信呢? 当我们两个directive嵌套使用的时候怎么保证子directive不会被 ...
- AngularJS中Directive间交互实现合成
假设需要烹饪一道菜肴,有3种原料,可以同时使用所有的3种原料,可以使用其中2种,也可以使用其中1种. 如果以Directive的写法,大致是:<bread material1 material2 ...
- AngularJS中Directive指令系列
近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用. &, <, =, @ 符 ...
随机推荐
- Spring中HttpInvoker远程方法调用总结
Spring为各种远程訪问技术的集成提供了工具类.Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程訪问功能的服务变得相当easy. 眼下,Spring支持四种远程技术: ...
- Axure9 v9.0.0.3629 ~ v9.0.0.3633 授权密钥 【2019.02.05】
现在提供一个支持v9.0.0.3629.v9.0.0.3630.v9.0.0.3631.v9.0.0.3632.v9.0.0.3633的授权码(后续的Beta更新版本应该能继续使用) 被授权人:zd4 ...
- 用MathType编辑反三角函数的方法
在使用文档写数学类的文章时候,常常会涉及到一些数学公式,由于数学公式中包含了很多的数学符号,如果使用文档自带的公式编辑器往往会发现很多的符号都不全或者不符合自己的要求.这个时候就需要一款专业的数学公式 ...
- TF-IDF(词频-逆向文件频率)用于文字分类
SVM分类器:支持向量机Support Vector Machine. 一个普通的SVM就是一条直线,用来完美划分linearly separable的两类.解决线性 要解决非线性需要到高维处理: 核 ...
- CSV转PDF(C++)
CSV : Comma Separate Values 特点: 每条记录占一行 以逗号为分隔符 逗号前后的空格会被忽略 字段中包含有逗号,该字段必须用双引号括起来 字段中包含有换行符,该字段必须用双引 ...
- iOS 断当前时间是否在一天的某个时间段内。
应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟 ...
- Angular ui-route的用法
ui-router和同属AngularJS框架一部分的ng-route一样强大. ui-router提供了让我们可以做路由嵌套和视图命名的特性,嵌套路由功能主要是依赖$stateProvider服务, ...
- 巨蟒python全栈开发-第24天 内置常用模块3
一. 1.re模块基础知识 2.python模块&re正则模块 3.实战:re模块&python(链家&电影天堂&豆瓣) 复习:上节课重点(1)sys.path 模块的 ...
- VS2008里的代码如何格式化
选中要格式化的代码, 先按Ctrl+K 再按Ctrl+F 从菜单中也可以 "编辑"->"高级"->"设置文档的格式Ctrl+K Ctrl+ ...
- python split(),os.path.split()和os.path.splitext()函数用法
https://blog.csdn.net/T1243_3/article/details/80170006 # -*- coding:utf-8 -*- """ @ ...