最近做了一個選擇標(biāo)簽的功能,把一些標(biāo)簽展示給用戶,用戶選擇自己喜歡的標(biāo)簽,就類似我們在購物網(wǎng)站看到的那種過濾標(biāo)簽似的;
簡單的效果如圖所示:
首先看一下html代碼:
1 <!DOCTYPE html> 2 <html data-ng-app="App"> 3 <head> 4 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> 5 <script src="script2.js"></script> 6 </head> 7 <body data-ng-controller="AddStyleCtrl"> 8 9 <div>Choose Tags</div> 10 <div>11 <div>You have choosen:</div>12 <hr>13 <label data-ng-repeat="selectedTag in selectedTags">14 (({{selectedTag}}))15 </label>16 <hr>17 <div data-ng-repeat="category in tagcategories">18 <div>{{ category.name }}</div>19 <div data-ng-repeat="tag in category.tags">20 <div>21 <input type="checkbox" id={{tag.id}} name="{{tag.name}}" ng-checked="isSelected(tag.id)" ng-click="updateSelection($event,tag.id)">22 {{ tag.name }}23 </div>24 </div>25 <hr>26 </div>27 </div>28 29 <pre>{{selected|json}}</pre>30 <pre>{{selectedTags|json}}</pre>31 32 </body>33 </html>
line2 定義了AngularJS App;
line4 引入angularjs腳本;
line5 引入自己寫的script2.js腳本;
line7 指定控制器AddStyleCtrl
line13-15 實時顯示已選標(biāo)簽給用戶看;
line17-line26 使用雙重循環(huán)列出數(shù)據(jù)庫(本例中就存儲在了controller的一個對象里)中的數(shù)據(jù);
line21的這行代碼作用可大了:<input type="checkbox" id={{tag.id}} name="{{tag.name}}" ng-checked="isSelected(tag.id)" ng-click="updateSelection($event,tag.id)">
存儲了tag的id,name,利用isSelected(tag.id)來判斷是否被checked,點擊時候調(diào)用updateSelection($event,tag.id)方法;
如果想 ng-click 觸發(fā)的函數(shù)里獲取到該觸發(fā)該函數(shù)的元素不能直接傳入 this ,而需要傳入 scope 。我們可以傳入 event.target 來獲取到該元素。
line29-30 是測試時候給自己看的,可以看到selected數(shù)組和selectedTags數(shù)組中的內(nèi)容;
然后看看AngularJS代碼:(script2.js)
1 /** 2 * Created by zh on 20/05/15. 3 */ 4 // Code goes here 5 6 var iApp = angular.module("App", []); 7 8 9 10 iApp.controller('AddStyleCtrl', function($scope)11 {12 $scope.tagcategories = [13 {14 id: 1,15 name: 'Color',16 tags: [17 {18 id:1,19 name:'color1'20 },21 {22 id:2,23 name:'color2'24 },25 {26 id:3,27 name:'color3'28 },29 {30 id:4,31 name:'color4'32 },33 ]34 },35 {36 id:2,37 name:'Cat',38 tags:[39 {40 id:5,41 name:'cat1'42 },43 {44 id:6,45 name:'cat2'46 },47 ]48 },49 {50 id:3,51 name:'Scenario',52 tags:[53 {54 id:7,55 name:'Home'56 },57 {58 id:8,59 name:'Work'60 },61 ]62 }63 ];64 65 $scope.selected = [];66 $scope.selectedTags = [];67 68 var updateSelected = function(action,id,name){69 if(action == 'add' && $scope.selected.indexOf(id) == -1){70 $scope.selected.push(id);71 $scope.selectedTags.push(name);72 }73 if(action == 'remove' && $scope.selected.indexOf(id)!=-1){74 var idx = $scope.selected.indexOf(id);75 $scope.selected.splice(idx,1);76 $scope.selectedTags.splice(idx,1);77 }78 }79 80 $scope.updateSelection = function($event, id){81 var checkbox = $event.target;82 var action = (checkbox.checked?'add':'remove');83 updateSelected(action,id,checkbox.name);84 }85 86 $scope.isSelected = function(id){87 return $scope.selected.indexOf(id)>=0;88 }89 });
line6 定義了angular app;
line10 定義了控制器AddStyleCtrl;
line12-63 定義了 標(biāo)簽對象
line64,66 聲明了$scope中的兩個數(shù)組對象(可以合并為1個),分別用來存儲tag的id和name;
line68-78 定義了updateSelected方法,這個方法會被updateSelection調(diào)用;
line69-72:如果add操作且 ‘?dāng)?shù)組[id]’ 元素不存在,向數(shù)組中添加數(shù)據(jù)(id,name);
line73-77:如果remove操作且‘?dāng)?shù)組[id]’ 元素存在,從數(shù)組中刪除數(shù)據(jù)(id,name);
line80-84定義了updateSelection方法,這個方法會在html頁面的checkbox被點擊時調(diào)用;
line81通過$event變量來獲取點擊的dom元素;
line82通過checkbox的當(dāng)前狀態(tài)來決定是add操作還是remove操作;
line83調(diào)用updateSelected方法,更新數(shù)據(jù);
line86-88定義了isSelected方法,用來判斷ID為id的checkbox是否被選中,然后傳值給頁面的ng-checked指令;