我们来创建一个自定义指令
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}</style></head><bodyng-app="myApp">
<divng-controller="mainCtrl">
<my-btn></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp.controller('mainCtrl',['$scope',function($scope){
$scope.myClass='primary';
}]);
myApp.directive('myBtn',function(){return{
template:'<inputtype="button"value="按钮"class="{{myClass}}">'
}
});</script></body></html>
1.webp
使用自定义指令像上面一样的确不错,但是如果你想要对每一个指令渲染出来的按钮定制化,则好像不可以,比如下面我们创建一堆这个自定义指令,他们长得一模一样:
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}</style></head><bodyng-app="myApp">
<divng-controller="mainCtrl">
<my-btn></my-btn>
<my-btn></my-btn>
<my-btn></my-btn>
<my-btn></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp.controller('mainCtrl',['$scope',function($scope){
$scope.myClass='primary';
}]);
myApp.directive('myBtn',function(){return{
template:'<inputtype="button"value="按钮"class="{{myClass}}">'
}
});</script></body></html>
2.webp
一种思路是把这几个自定义的指令按钮放到不同的控制器里面,然后控制器里通过$scope上下文传递不同的值:
<!DOCTYPEhtml><html><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:green;
}.default{background:gray;
}</style></head><bodyng-app="myApp">
<divng-controller="aCtrl">
<my-btn></my-btn>
</div>
<divng-controller="bCtrl">
<my-btn></my-btn>
</div>
<divng-controller="cCtrl">
<my-btn></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp.controller('aCtrl',['$scope',function($scope){
$scope.myClass='primary';
}]);
myApp.controller('bCtrl',['$scope',function($scope){
$scope.myClass='success';
}]);
myApp.controller('cCtrl',['$scope',function($scope){
$scope.myClass='default';
}]);
myApp.directive('myBtn',function(){return{
template:'<inputtype="button"value="按钮"class="{{myClass}}">'
}
});</script></body></html>
3.webp
这样写太麻烦了,所以我们的angular为我们的自定义指令提供了一个配置项叫scope,所以,我们可以如下这样写:
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:green;
}.default{background:gray;
}</style></head><bodyng-app="myApp">
<divng-controller="Controller">
<my-btnb="className1"></my-btn>
<my-btnb="className2"></my-btn>
<my-btnb="className3"></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp
.controller('Controller',['$scope',function($scope){
$scope.className1='primary';
$scope.className2='success';
$scope.className3='default';
}])
.directive('myBtn',function(){return{
scope:{
a:'=b'
},
template:'<inputtype="button"value="按钮"class="{{a}}">'
}
});</script></body></html>
要看懂上面的只要注意两点:
这里的独立作用域里面的a代表的是template里面的模型a
=b代表的是要angular去寻找视图里面的当前指令的属性b
属性b的值需要去外部作用域里面去寻找
如果你想在指令作用域里绑定的模型的名字和外部使用的时候的属性名一样,可以省写成如下:
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:green;
}.default{background:gray;
}</style></head><bodyng-app="myApp">
<divng-controller="Controller">
<my-btna="className1"></my-btn>
<my-btna="className2"></my-btn>
<my-btna="className3"></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp
.controller('Controller',['$scope',function($scope){
$scope.className1='primary';
$scope.className2='success';
$scope.className3='default';
}])
.directive('myBtn',function(){return{
scope:{
a:'='
},
template:'<inputtype="button"value="按钮"class="{{a}}">'
}
});</script></body></html>
当然,上面的=号是双向数据绑定:
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:green;
}.default{background:gray;
}</style></head><bodyng-app="myApp">
<divng-controller="Controller">
<my-btna="abc"></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp
.controller('Controller',['$scope',function($scope){
$scope.abc='我是初始内容';
}])
.directive('myBtn',function(){return{
scope:{
a:'='
},
template:'<inputtype="text"ng-model="a"><span>{{a}}</span>'
}
});</script></body></html>
如果只是想单向的数据通信,可以用@符号:
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:red;
}.default{background:red;
}</style></head><bodyng-app="myApp">
<divng-controller="Controller">
<my-btna="primary"></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp
.controller('Controller',['$scope',function($scope){
$scope.mm='primary';
}])
.directive('myBtn',function(){return{
scope:{
a:'@'
},
template:'<inputtype="button"value="按钮"class="{{a}}">'
}
});</script></body></html>
如果想用ng-class,也是可以的:
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:red;
}.default{background:red;
}</style></head><bodyng-app="myApp">
<divng-controller="Controller">
<my-btna="primary"></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp
.controller('Controller',['$scope',function($scope){
$scope.mm=true;
}])
.directive('myBtn',function(){return{
scope:{
a:'@'
},
template:'<inputtype="button"value="按钮"ng-class="{primary:a}">'
}
});</script></body></html>
最后,还有一个scope可以设置是引用外部作用域的方法
<!DOCTYPEhtml><htmllang="en"><head>
<metacharset="UTF-8">
<title>Document</title>
<style>
.primary{background:red;
}.success{background:red;
}.default{background:red;
}</style></head><bodyng-app="myApp">
<divng-controller="Controller">
<my-btnfn2="fn()"></my-btn>
</div>
<scriptsrc="node_modules/angular/angular.min.js"></script>
<script>
varmyApp=angular.module('myApp',[]);
myApp
.controller('Controller',['$scope',function($scope){
$scope.fn=function(){
alert(11);
}
}])
.directive('myBtn',function(){return{
scope:{
fn1:'&fn2'
},
template:'<inputtype="button"value="按钮"ng-click="fn1()">'
}
});</script></body></html>
本文转载自中文网 |