標籤:
目錄
- API概覽
- 使用Angular.UI.Bootstrap
- 自訂指令
- 我的指令
angular中的指令可謂是最複雜的一塊
但是我們的上傳組件就能這麼寫
:
API概覽
先上一段虛擬碼:
angular.module(‘moduleName‘, []).directive( ‘namespaceDirectiveName‘, [ function() { return { restrict : ‘‘,// 描述指令在模版中的使用方式,包括元素E,屬性A,CSS樣式類,注釋或者以上方式的任意主和 priority : 0,// 設定指令在模版中的執行順序,順序是相對於其他指令而言 template : ‘‘,// 以字串的形式編寫一個內聯模版,如果以url的形式提供模版,此屬性會被忽略 templateUrl : ‘‘,// 描述載入模版所需要的url。如果使用temlate形式提供模版,此屬性會被忽略 replace : true,// 如果設定為true則替換指令所在的元素,否則就追加到元素內部 transclude : true,// 把指令元素原來的子節點移動到一個新模版內部 scope : ‘bool or object‘,// 為當前指令建立一個新的範圍,而不是使之繼承父範圍 constroller : function($scope, $element, $attrs, $transclude) { // 建立一個控制器,它會暴露一個API,利用這個API可以在多個指令之間進行通訊 }, require : ‘‘,// 要求必須存在另個一指令,當前指令才能執行 link : function(scope, iElement, iAttrs) { // 使用編程的方式修改最終產生的dom元素的執行個體,添加事件監聽器,並設定資料繫結 }, compile : function(tElement, tAttrs, transclude) { //在使用ng-repeat用編程的方式修改dom模版,從而實現一個指令跨越多個指令的特性。 //也可以返回一個link函數,可以用它來修改產生元素的樣本 return { pre : function preLink(scope, iElement, iAttrs, controller) { }, post : function postLink(scope, iElement, iAttrs, controller) { } } } }; } ]);
這裡重點介紹幾個定義
restrict: 描述指令在模版中的使用方式,包括:元素、樣式類、屬性、注釋,或者以上幾種方式的任意組合。(預設為AE)
使用AngularUI.Bootstrap
GitHub:https://angular-ui.github.io/bootstrap/
Nuget:
Install-Package Angular.UI.Bootstrap(該庫在Bootstrap3.3.7下測試通過,這裡順便說包中的帶tpls.js實際就是內建了模板的js)
這裡示範一個最常用到的Pager
<link href="Content/bootstrap.min.css" rel="stylesheet" /></head><body ng-app="ui.bootstrap.demo"> <div ng-controller="PaginationDemoCtrl"> <ul uib-pagination total-items="totalItems" previous-text="上頁" next-text="下頁" first-text="首頁" last-text="末頁" items-per-page="itemsPerPage" ng-model="currentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" force-ellipses="true" ng-change="pageChanged()"></ul> </div> <script src="Scripts/angular.min.js"></script> <script src="Scripts/angular-ui/ui-bootstrap-tpls.min.js"></script> <script> angular.module(‘ui.bootstrap.demo‘, [‘ui.bootstrap‘]).controller(‘PaginationDemoCtrl‘, function ($scope, $log) { $scope.totalItems = 1000; $scope.currentPage = 4; $scope.maxSize = 5;//顯示分頁碼個數 $scope.itemsPerPage = 20;//每頁大小 $scope.pageChanged = function () { $log.log(‘Page changed to: ‘ + $scope.currentPage); }; }); </script></body>
更多需要ui-bootstrap的其他外掛程式 訪問對應的GitHub Pages
自訂指令
對於restrict,template,replace,transclude並不複雜,本節不做過多贅述.
scope
scope預設false: 表示使用現有的scope
設定為true : 表示新的scope(會繼承父scope的屬性)
設定為{} : 表示獨立的scope(預設訪問不到父scope的屬性)
在設定為對象{}的時候,可以通過繫結原則傳遞父scope的屬性
scope為true和預設false時,觀察h2內容即可看到效果
<body ng-app="myApp" ng-init="user=‘aaa‘"> <h2>{{user}}</h2> <span hello></span> <script> angular.module(‘myApp‘, []) .directive(‘hello‘, [function () { return { scope: true, template: ‘<span>Hello-{{user}}</span>‘, link: function ($scope) { $scope.user = ‘ccc‘; } } }]); </script></body>
我們再探索下為scope為對象的時候,在上面的例子中做下調整,使用繫結原則@
<span hello="{{user}}"></span> <script> angular.module(‘myApp‘, []) .directive(‘hello‘, [function () { return { scope: { user: ‘@hello‘ }, template: ‘<span>Hello-{{user}}</span>‘, link: function ($scope) { $scope.user = ‘ccc‘; } } }]); </script>
發現和scope為false的時候效果一樣,其實這已經為完全獨立的scope了
我們再調整下繫結原則為=
<span hello="user"></span>
return { scope: { user: ‘=hello‘ }, template: ‘<span>Hello-{{user}}</span>‘, link: function ($scope) { $scope.user = ‘ccc‘; } }
最後我們看下&
<body ng-app="myApp" ng-controller="helloCtrl"> <span func="log(aa,bb)"></span> <script> angular.module(‘myApp‘, []).controller(‘helloCtrl‘, function ($scope) { $scope.log = function (name, age) { console.log(‘hello:‘ + name + ‘:‘ + age); } }).directive(‘hello‘, [function () { return { scope: { func: ‘&‘ }, template: ‘<span ng-click="func({aa:\‘小2\‘,bb:\‘19歲\‘})">Click Me</span>‘, link: function ($scope) { $scope.user = ‘ccc‘; $scope.func({ aa: ‘小M‘, bb: ‘18歲‘ }); } } }]); </script></body>
我想到此,scope已經ok.
link
談到link一般都會說說compile,但我這裡會重點介紹controller,require
link函數 會在指令的每個執行個體上觸發一次.
controller函數 一般用來在指令間傳遞資料
require函數 依賴其他的指令
我們來自訂一個‘經典的‘according
<body ng-app="myApp" ng-init="items=[{title:‘t1‘,text:‘x1‘},{title:‘t2‘,text:‘x2‘},{title:‘t3‘,text:‘x3‘}]"> <div hello> <div word title="item.title" ng-repeat="item in items">{{item.text}}</div> </div></body>
先定義hello 指令
angular.module(‘myApp‘, []) .directive(‘hello‘, [ function () { return { scope: { title: ‘=‘ }, transclude: true, replace: true, controller: function () { var words = []; this.add = function (word) { words.push(word); } this.openOne = function (word) { for (var i = 0; i < words.length; i++) { if (words[i] !== word) words[i].show = false; } } }, template: ‘<div ng-transclude></div>‘ } } ])
word指令
.directive(‘word‘, [ function () { return { require: ‘?^hello‘, scope: { title: ‘=‘ }, transclude: true, replace: true, template: ‘<div><div ng-click="toggle()">{{title}}</div><div ng-show="show" ng-transclude></div></div>‘, link: function (scope, ele, attr, ctrl) { if (!ctrl) { console.warn(‘無hello指令‘); return; } scope.show = false; ctrl.add(scope); scope.toggle = function () { scope.show = !scope.show; ctrl.openOne(scope); } } } } ]);
(這個according是不是太low了):
require
require接受字串或字串數組.會將對應的控制器注入到link函數的第4個參數上.
預設會在自身找的元素找指令
加^表示會從父級找指令
加?表示如果沒找到,傳遞null給link
通常為了防止報錯,會傳‘?^‘組合(不分先後)
這裡再補充下ngModel的情況.因為常常要和模型做互動
也就是require:‘?ngModel‘這種情況.
再補充下ngModelCtrl常用的屬性或方法:
$setViewValue(): 設定ngModel值
$formatters: 格式化模型顯示值
$render: 定義模型如何渲染展示方法
我的指令
雖然angular已經有很多指令外掛程式,但我還是忍不住寫了一些指令.
如分頁,檔案上傳,富文本等。
分享1個今天寫的fileinput 熱乎著呢。
Nuget:Install-Package angularjs.fileinput
GitHub: https://github.com/NeverCL/Angular.FileInput
同時推薦一篇很詳細的部落格:http://www.cnblogs.com/rohelm/p/4051437.html
本文地址:http://www.cnblogs.com/neverc/p/5916247.html
[AngularJS] AngularJS系列(4) 中級篇之指令