標籤:class dialog map tag modal cst dir function tle
正在初學angularjs中,在網上看到一篇詳細講解directive指令的文章,於是就記錄在這裡和大家一起分享
angular.module(‘docsTransclusionExample‘, [])
.controller(‘Controller‘, [‘$scope‘, function($scope) {
$scope.name = ‘Tobias‘;
}])
.directive(‘myDialog‘, function() {
return {
restrict: ‘E‘,
transclude: true,
scope: {},
templateUrl: ‘my-dialog.html‘,
link: function (scope, element) {
scope.name = ‘Jeff‘;
}
};
});
1.restrict
E: 表示該directive僅能以element方式使用,即:<my-dialog></my-dialog>
A: 表示該directive僅能以attribute方式使用,即:<div my-dialog></div>
EA: 表示該directive既能以element方式使用,也能以attribute方式使用
2.transclude
directive可能接受頁面上的其他html內容時才會用到,建議先去掉該參數。有些高階了。
3.scope
當你寫上該屬性時,就表示這個directive不會從它的controller裡繼承$scope對象,而是會重新建立一個
4.templateUrl
你的directive裡的html內容
5.link
可以簡單理解為,當directive被angular 編譯後,執行該方法.link中的第一個參數scope基本上就是上面寫的那個scope
6.element簡單說就是$(‘my-dialog‘)
attrs是個map,內容是你這個directive上的所有屬性,例如:你在頁面上如果這樣寫了directive:
<my-dialog type="modal" animation="fade"></my-dialog>
那attrs就是:
{
type: ‘modal‘,
animation: ‘fade‘
}
angularjs中的directive