详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
(编辑:jimmy 日期: 2025/5/15 浏览:3 次 )
对于一个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="/UploadFiles/2021-04-02/angular.js">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
上一篇:JS轮播图实现简单代码<!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="/UploadFiles/2021-04-02/angular.js">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="/UploadFiles/2021-04-02/angular.js">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>' } });尝试一下,就明白了,简洁明了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:JavaScript实现的数字与字符串转换功能示例
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。