【问题标题】:Filtering AngularJS inside the scope在范围内过滤 AngularJS
【发布时间】:2013-11-08 03:16:21
【问题描述】:

我正在尝试对我的代码应用某种过滤器,但是当我单击菜单时它并没有改变任何东西。我最初需要显示我的所有专辑,如果用户点击其中一位艺术家,我想过滤它们。下面是我的控制器:

Function ListCtrl($scope, $http) {

$http({
    method: 'GET',
    url: 'json/json_price_1.json'
}).success(function(data) {
    $scope.artists = data.artists; // response data

    $scope.albums = [];
    $scope.currentArtist = null;
    $scope.setArtist = function(name) {
        $scope.currentArtist = $scope.artists[name];
    };
    if ($scope.currentArtist == null) {
        angular.forEach($scope.artists, function(element, index) {
            angular.forEach(element.albums, function(album, index) {
                $scope.albums.push(album);
            });
        });
    } else {
        angular.forEach($scope.currentArtist.albums, function(album, index) {
            $scope.albums.push(album);
        });
    };

});};

我的 html 如下:

<div ng-controller="ListCtrl">  
<ul class="topcoat-list" ng-repeat="artist in artists">
    <li class="topcoat-list__item">
        <a href="" ng-click="setArtist(artist.name)">
            {{artist.name}}
        </a>
    </li>   
</ul></div>
<div ng-controller="ListCtrl">
<ul ng-repeat="album in albums">
  <li>{{album.title}}</li>
</ul>

感谢你们的时间!

【问题讨论】:

    标签: javascript angularjs cordova filter


    【解决方案1】:

    不是angularjs专家,问题是你没有在设置艺术家后过滤专辑。

    Function ListCtrl($scope, $http) {
    
        $scope.setArtist = function (name) {
            $scope.currentArtist = $scope.artists[name];
            $scope.filter();
        };
    
        $scope.filter = function () {
            $scope.albums = [];
            if (!$scope.currentArtist) {
                angular.forEach($scope.artists, function (element, index) {
                    angular.forEach(element.albums, function (album, index) {
                        $scope.albums.push(album);
                    });
                });
            } else {
                angular.forEach($scope.currentArtist.albums, function (album, index) {
                    $scope.albums.push(album);
                });
            };
    
        }
    
    
        $http({
            method: 'GET',
            url: 'json/json_price_1.json'
        }).success(function (data) {
            $scope.artists = data.artists; // response data
    
            $scope.currentArtist = undefined;
            $scope.filter()
    
        });
    };
    

    角度解决方案是使用filter - 类似于this demo

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 1970-01-01
      • 2014-01-05
      • 2017-02-15
      • 2014-05-21
      • 1970-01-01
      • 2013-04-23
      • 2016-08-20
      • 1970-01-01
      相关资源
      最近更新 更多