详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
- 作者: 彬77225821
- 来源: 51数据库
- 2021-09-03
对于一个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>' } });
尝试一下,就明白了,简洁明了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4