angular的scopel指令使用詳細說明
發(fā)表時間:2023-09-13 來源:明輝站整理相關軟件相關文章人氣:
[摘要]這次給大家?guī)韆ngular的scopel指令使用詳解,使用angular的scopel指令注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。我們來創(chuàng)建一個自定義指令<!DOCTYPE html><html lang="en"><head>&l...
這次給大家?guī)韆ngular的scopel指令使用詳解,使用angular的scopel指令注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。
我們來創(chuàng)建一個自定義指令
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} </style></head><body ng-app="myApp">
<div ng-controller="mainCtrl">
<my-btn></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('mainCtrl',['$scope',function($scope){
$scope.myClass = 'primary';
}]);
myApp.directive('myBtn',function(){ return {
template:'<input type="button" value="按鈕" class="{{myClass}}">'
}
}); </script></body></html>
使用自定義指令像上面一樣的確不錯,但是如果你想要對每一個指令渲染出來的按鈕定制化,則好像不可以,比如下面我們創(chuàng)建一堆這個自定義指令,他們長得一模一樣:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} </style></head><body ng-app="myApp">
<div ng-controller="mainCtrl">
<my-btn></my-btn>
<my-btn></my-btn>
<my-btn></my-btn>
<my-btn></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('mainCtrl',['$scope',function($scope){
$scope.myClass = 'primary';
}]);
myApp.directive('myBtn',function(){ return {
template:'<input type="button" value="按鈕" class="{{myClass}}">'
}
}); </script></body></html>
一種思路是把這幾個自定義的指令按鈕放到不同的控制器里面,然后控制器里通過$scope上下文傳遞不同的值:
<!DOCTYPE html><html><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: green;
} .default{ background: gray;
} </style></head><body ng-app="myApp">
<div ng-controller="aCtrl">
<my-btn></my-btn>
</div>
<div ng-controller="bCtrl">
<my-btn></my-btn>
</div>
<div ng-controller="cCtrl">
<my-btn></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = 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:'<input type="button" value="按鈕" class="{{myClass}}">'
}
}); </script></body></html>
這樣寫太麻煩了,所以我們的angular為我們的自定義指令提供了一個配置項叫scope,所以,我們可以如下這樣寫:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: green;
} .default{ background: gray;
} </style></head><body ng-app="myApp">
<div ng-controller="Controller">
<my-btn b="className1"></my-btn>
<my-btn b="className2"></my-btn>
<my-btn b="className3"></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = 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:'<input type="button" value="按鈕" class="{{a}}">'
}
}); </script></body></html>
要看懂上面的只要注意兩點:
這里的獨立作用域里面的a代表的是template里面的模型a
=b代表的是要angular去尋找視圖里面的當前指令的屬性b
屬性b的值需要去外部作用域里面去尋找
如果你想在指令作用域里綁定的模型的名字和外部使用的時候的屬性名一樣,可以省寫成如下:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: green;
} .default{ background: gray;
} </style></head><body ng-app="myApp">
<div ng-controller="Controller">
<my-btn a="className1"></my-btn>
<my-btn a="className2"></my-btn>
<my-btn a="className3"></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = 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:'<input type="button" value="按鈕" class="{{a}}">'
}
}); </script></body></html>
當然,上面的=號是雙向數據綁定:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: green;
} .default{ background: gray;
} </style></head><body ng-app="myApp">
<div ng-controller="Controller">
<my-btn a="abc"></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp
.controller('Controller', ['$scope', function($scope) {
$scope.abc = '我是初始內容';
}])
.directive('myBtn',function(){ return {
scope:{
a:'='
},
template:'<input type="text" ng-model="a"><span>{{a}}</span>'
}
}); </script></body></html>
如果只是想單向的數據通信,可以用@符號:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: red;
} .default{ background: red;
} </style></head><body ng-app="myApp">
<div ng-controller="Controller">
<my-btn a="primary"></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp
.controller('Controller', ['$scope', function($scope) {
$scope.mm = 'primary';
}])
.directive('myBtn',function(){ return {
scope:{
a:'@'
},
template:'<input type="button" value="按鈕" class="{{a}}">'
}
}); </script></body></html>
如果想用ng-class,也是可以的:
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: red;
} .default{ background: red;
} </style></head><body ng-app="myApp">
<div ng-controller="Controller">
<my-btn a="primary"></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp
.controller('Controller', ['$scope', function($scope) {
$scope.mm = true;
}])
.directive('myBtn',function(){ return {
scope:{
a:'@'
},
template:'<input type="button" value="按鈕" ng-class="{primary:a}">'
}
}); </script></body></html>
最后,還有一個scope可以設置是引用外部作用域的方法
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.primary{ background: red;
} .success{ background: red;
} .default{ background: red;
} </style></head><body ng-app="myApp">
<div ng-controller="Controller">
<my-btn fn2="fn()"></my-btn>
</div>
<script src="node_modules/angular/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp
.controller('Controller', ['$scope', function($scope) {
$scope.fn = function(){
alert(11);
}
}])
.directive('myBtn',function(){ return {
scope:{
fn1:'&fn2'
},
template:'<input type="button" value="按鈕" ng-click="fn1()">'
}
}); </script></body></html>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
Angular Material的使用詳解
css中id選擇器的命名規(guī)則有哪些
讓元素水平垂直居中冷門方法
以上就是angular的scopel指令使用詳解的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。