简体   繁体   English

AngularJS ng-repeat过滤来自JSON的特定ID

[英]AngularJS ng-repeat filter by specific ID from JSON

I cant seem to make this work. 我似乎无法使这项工作。 I've been trying to filter a JSON file by specific ID then iterate using ng-repeat. 我一直在尝试按特定ID过滤JSON文件,然后使用ng-repeat进行迭代。

here's what I've tried 这是我尝试过的

here's the button trigger: 这是按钮触发器:

<a href="#/compare-details"><button class="viewDetails" ng-click="passID(11,02,00)" type="button">test button</button></a>

after passing the 3 id parameters, it goes to my controller: 传递3个id参数后,它会进入我的控制器:

var newData;
angular.module('handsetExplorerModule')
.controller("compareDetailsController", ['$scope','compareDetailsService', function($scope, compareDetailsService) {

   compareDetailsService.getHandsetList()
   .then(
       function(data){

         $scope.passID = function(id1,id2,id3){

             for(int i=0; i<data.phones.length; i++){
                if(data.phones[i].id == id1 || data.phones[i].id == id2 || data.phones[i].id == id3){
                     newData += data.phones[i];
                 }   
             }
         }
       }, 
       function(error){
           //todo: handle error
       }
    );

     $scope.phoneContent = newData;

}]);

JSON file came from service: JSON文件来自服务:

angular.module('handsetExplorerModule')
.factory('compareDetailsService', ['$http','$q', function($http, $q) {
   var service = {
   };
   service.getHandsetList = function(){
     var deferred = $q.defer();
      $http.get("./data/compareJSON.json").then(function(response) {
            deferred.resolve(response.data);
    },
        function(response){
            deferred.reject("ERROR: Unable to get handsetList data");
        }
    );
    return deferred.promise;
   };

   return service;
 }]);

and my ng-repeat 和我的重复

<div id="divContent">
            <div>
            <table id="Content"  ng-repeat="x in phoneContent">
            <tr>
                <td class="one">
                    <table>
                    <tr>
                        <td><img class="contImage" ng-src="{{x.image}}" ng-alt="{{x.name}}" /></td>
                        <td class="textAlign">{{x.name}} <button class="viewDetails" ng-click="viewDetails(x.id)" type="button">VIEW DETAILS</button></td>
                    </table>
                </td>   
                <td class="two">{{x.size}}</td>
                <td class="one">{{x.storage}}</td>
                <td class="two">{{x.camera}}</td>
                <td class="one">{{x.battery}}</td>
                <td class="two">{{x.network}}</td>
                <td class="one">{{x.sim}}</td>
            </tr>
            </table>
            </div>      
</div>

please check refactored a little bit plunker: http://plnkr.co/edit/paPSj8Dm4Z1RCbFw9FmS?p=preview 请检查重构一点点plunker: http ://plnkr.co/edit/paPSj8Dm4Z1RCbFw9FmS?p = preview

BTW, $http service by its self is a Promise, so no need to use $q service, just return $http : BTW, $ http服务由它自己是Promise,所以不需要使用$ q服务,只需返回$ http

myApp.factory('compareDetailsService', ['$http', function($http) {
  return {
    getHandsetList: function(){
      return $http.get("data.json");
    }
  };
}]);

there is a very similar tutorial, you should look at: https://docs.angularjs.org/tutorial/step_00 有一个非常类似的教程,你应该看看: https//docs.angularjs.org/tutorial/step_00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM