2016-11-11 68 views
7

我正在採取一個早該過期的Angular研討會,並試圖將常規JQuery的頁面轉換爲Angular功能。我堅持的一件事是用ng-click激活一個過濾器(至少我認爲這是首選方法)。帶角度的選項卡過濾

所以基本上我有一個畫廊和一個菜單左側的圖庫中的這些圖像的類別(組)。小組根據左邊的菜單顯示。所以,4組(女孩,自然,音樂,空間)。圖像被分成這些組。如果我按下女孩,我應該只看到小組中的圖像:女孩,如果我按大自然,我只能看到羣組中的照片:大自然等,如果我按全部,我應該看到他們。

嗯,它不工作。儘管我遵循這個How to filter a list in AngularJS using several links

這裏是一個可視化的頁面給一個想法

這裏是我的代碼

HTML

<div class="content-body" ng-controller="galleryController as panel"> 
    <div class="col-xs-12 col-md-3"> 
    <div class="content-sidebar"> 
     <div class="side-widget menu"> 
     <h4>Groups:</h4> 
     <div class="border-bottom"> 
      <ul ng-init="tab = 1" class="list-group"> 
      <li ng-class="{active:panel.isSelected(1)}"> 
       <a class="list-group-item" ng-click="panel.selectTab(1); groupFilter = {}">All <span 
              class="badge badge-primary">12</span></a> 
      </li> 
      <li ng-class="{active:panel.isSelected(2)}"> 
       <a class="list-group-item" ng-click="panel.selectTab(2); groupFilter ={group:'nature'}">Nature <span 
               class="badge badge-success">7</span></a> 
      </li> 
      <li ng-class="{active:panel.isSelected(3)}"> 
       <a class="list-group-item" ng-click="panel.selectTab(3)">Music <span 
               class="badge badge-danger">3</span></a> 
      </li> 
      <li ng-class="{active:panel.isSelected(4)}"> 
       <a class="list-group-item" ng-click="panel.selectTab(4)">Space <span 
               class="badge badge-info">2</span></a> 
      </li> 
      <li ng-class="{active:panel.isSelected(5)}"> 
       <a class="list-group-item" ng-click="panel.selectTab(5)">Girls <span 
               class="badge badge-warning">3</span></a> 
      </li> 
      </ul> 
     </div> 
     </div> 
    </div> 
    </div> 
    <div class="col-xs-12 col-md-9"> 
    <div class="gallery"> 
     <div> 
     <div class="col-xs-12 col-md-3" ng-repeat="gallery in galleries | filter:groupFilter"> 
      <a class="gallery-item" ng-href="{{gallery.img}}" ng-class="{true:'active',false:''}[checked]" 
       title="Nature Image 1" > 
       <div class="image"> 
        <img ng-src="{{gallery.img}}" alt="Nature Image 1"/> 

       </div> 
       <div class="meta"> 
        <strong>{{gallery.title}}</strong> 
        <span>{{gallery.desc}}</span> 
       </div> 
      </a> 
      <ul class="gallery-item-controls"> 
       <li><label class="check"><input type="checkbox" class="icheckbox" ng-model="checked" /></label></li> 
       <li><span class="gallery-item-remove"><i class="fa fa-times" ng-click="removeGalleryItem(galleryItem, $event)"></i></span></li> 
      </ul> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 

控制器,如果你需要看到它

app.controller('galleryController', ['$scope', '$http', function($scope, $http) { 
    $http.get('data/galleries.json').success(function(data){ 
     $scope.galleries = data; 
    }); 

    $scope.removeGalleryItem=function(gallery){ 
     var removedGallery = $scope.galleries.indexOf(gallery); 
     $scope.galleries.splice(removedGallery, 1); 
    }; 

    this.tab = 1; 

    this.selectTab = function(setTab){ 
     this.tab = setTab; 
    }; 

    this.isSelected = function(checkTab){ 
     return this.tab === checkTab; 
    }; 



}]); 

數據

[ 
{ 
    "img":"assets/images/gallery/girls-1.jpg", 
    "group": "girls", 
    "title": "Image 1", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/girls-2.jpg", 
    "group": "girls", 
    "title": "Image 2", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/girls-3.jpg", 
    "group": "girls", 
    "title": "Image 3", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/music-1.jpg", 
    "group": "music", 
    "title": "Image 4", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/music-2.jpg", 
    "group": "music", 
    "title": "Image 5", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/music-3.jpg", 
    "group": "music", 
    "title": "Image 6", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/music-4.jpg", 
    "group": "music", 
    "title": "Image 7", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/music-5.jpg", 
    "group": "music", 
    "title": "Image 8", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-1.jpg", 
    "group": "nature", 
    "title": "Image 9", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-2.jpg", 
    "group": "nature", 
    "title": "Image 10", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-3.jpg", 
    "group": "nature", 
    "title": "Image 11", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-4.jpg", 
    "group": "nature", 
    "title": "Image 12", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-5.jpg", 
    "group": "nature", 
    "title": "Image 13", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-6.jpg", 
    "group": "nature", 
    "title": "Image 14", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/nature-7.jpg", 
    "group": "nature", 
    "title": "Image 15", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/space-1.jpg", 
    "group": "space", 
    "title": "Image 16", 
    "desc": "Description", 
    "link":"" 
}, 
{ 
    "img":"assets/images/gallery/space-2.jpg", 
    "group": "space", 
    "title": "Image 17", 
    "desc": "Description", 
    "link":"" 
} 
] 

以防萬一你想知道什麼是在控制器中。一個刪除畫廊項目也不工作,但這是另一個問題。還有一些功能可以將活動類添加到菜單中,以便顯示它何時處於活動狀態。

根據上面的教程,控制器中沒有任何東西需要過濾工作,因爲它全部包含在Angular中。

編輯
任何想法如何使從app.js文件中分離數據這filetering工作。

這是一個碼簿的link。正如你可以看到它的工作原理,如果我把數據放在應用程序文件中,但不是當我遠程它(因爲它應該是)我需要建立一個工廠?在app.js註釋掉數據和遠程離開到包含JSON和圖像的回購,看到它失敗

enter image description here

+0

錯誤只是想你的代碼在這裏:https://jsfiddle.net/kqrpz2mq/工作方式和組=自然,你已經實現了只有一個魅力 – yunandtidus

+0

它確實讓我很高興看到最終過濾本質上沒有錯,但它仍然無法按預期工作。我添加了一個codepen,顯示它在app.js文件中添加數據時工作,但當我連接到外部數據時失敗。我需要將數據分開。最終這些數據將作爲數據庫 – LOTUSMS

回答

7

在OP的要求我加入這裏的答案是相似我爲另一個問題寫了一篇文章。

注到OP - 也許編輯它,以便未來的讀者不會混淆:-)


做一些研究,我認爲這個問題是galleryController在您的標記定義的地方,但後圖庫中的元素不在控制器定義的位置。

參照http://joli.sitedev.online/#/gallery。在文件slidesController。JS那裏galleryController定義我把一個破發在這裏和代碼暫停:

enter image description here

我也把一個破發點這裏,但在一個刪除按鈕,當點擊該代碼不會暫停:

enter image description here

望着標記我看不到任何ng-controller="galleryController"跡象,所以我不能看到畫廊ng-repeat如何填充:

enter image description here

也許是通過這個:

enter image description here

如果通過那就說明事情作爲指令有其自己的控制器。任何進一步的信息將有所幫助,我相信我們可以清除這一點。

+0

保留點。你對另一個問題的回答適用於這個問題,但他們不是一回事。但是,可能來這裏的人會發現這個答案對這個問題很有幫助。再次感謝芽! – LOTUSMS

1

這是因爲交叉生產地

跨域資源共享(CORS)是一種機制,允許在網頁上限制資源(如字體)從另一個域要求外域,從中資源來源。 1網頁可以自由嵌入圖像,樣式表,腳本,iframe,視頻。[2]某些「跨域」請求,特別是AJAX請求,由同一安全策略默認禁止。

如果Inspet你的瀏覽器,你可以看到cross orign

+0

http://plnkr.co/edit/ajiIESeZ8u0f0koa8LJt?p=info – nmanikiran