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

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

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

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

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

html

rush:xhtml;">

<div ng-controller="listCtrl">

我的angularjs

js

rush:js;"> var myApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore="motorola"; });

myApp.directive('test',function(){
return {
'restrict':'E',scope:{
title:"@"
},template:'

{{title}}
'

}
});

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

2. = 的使用。

html

rush:xhtml;">

<div ng-controller="listCtrl">

{{title}}

我的angularjs

js

rush:js;"> var myApp=angular.module('myApp',scope:{ title:"=" },template:'
{{title}}
'

}
});

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

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

html

rush:xhtml;">

<div ng-controller="listCtrl">

js

rush:js;"> var myApp=angular.module('myApp',function($scope){ $scope.logchore=function(){ alert('ok'); }; });

myApp.directive('test',scope:{
flavor:"&"
},template:'

<button ng-click="flavor()">
'

}
});

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...