angularjs中的directive

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.