【问题标题】:angular listing not working when i compare two fields?当我比较两个字段时,角度列表不起作用?
【发布时间】:2015-08-09 10:24:03
【问题描述】:
<div  ng-controller="SamsungServicesCtrl">
     <ion-content> 
           <li class="item item-checkbox"  ng-repeat="item in items" >
             <img src="{{item.icon}}"  style="float:left;height:30px;width:30px;padding-right:5px;" > 
             <label  class="checkbox checkbox-energized"  >
               <input type="checkbox"  style="float:right;"   ng-model="item.selected"  >
             </label>
             {{item.name}}
              <p style="font-size:10px;  padding-left:0px;">  {{item.subname}}</p>
           </li>
           <div class="padding">
            <button class="button button-full button-energized"  value="submit" ng-click="check()">Apply
            </button>
           </div>

    </ion-content>
</div>
<script>
.controller('SamsungServicesCtrl', function ($scope,$filter,$location) {
       var service = ["Regular Service", "Settings Faults", "Software Faults", "Hardware Faults"];
       var services = ["General services", "If a phone doesn’t switch-on, it is called a dead phone, SIM card does not get detected", "Hardware Faults"];
       var icons = ["images/arrow.svg","images/arrow.svg","images/arrow.svg"];
         $scope.items= [] ;
            for(var I=0;I<service.length;I++)
                {
                    var modal = {
                        name:service[I],
                        subname:services[I],
                        icon:icons[I],
                        selected:false
                      }; 

                    $scope.items.push(modal);        
                }
                scope.check = function()
                {
                      var checkedItems = [];
                       for(var I=0;I<$scope.items.length;I++){
                          if($scope.items[I].selected){
                             checkedItems.push($scope.items[I].name);
                             }
                            }


        //I have checked Regular Service, Settings Faults
        $location.path('listing').search({model: 'samsung', items:checkedItems});
                 }

    })  
</script>

我已经给了我的服务页面 html 和控制器。在此页面中包含服务列表,这是我的第一页,用户需要选择其中任何一项服务。用户单击应用按钮后,我将重定向到包含所选服务的列表页面:商店类型(诺基亚,三星)($location.path('listing').search({model:'samsung',items:checkedItems}))。这里用户只选择了服务,但我将型号名称附加为三星。

<div class="bar bar-header item-input-inset">
    <label class="item-input-wrapper" >
         <I class="icon ion-ios-search placeholder-icon"></I>
         <input type="text" placeholder="Search" ng-model="query" >
    </label>
</div>
<div class="list card"  data-ng-repeat="dealer in dealers |  filter:query ">
    <div class="item item-thumbnail-left item-icon-right" href="#">
         <h2>{{dealer.Store_Name}}</h2> 
         <p>{{dealer.S_Address.area}} {{dealer.S_Address.city}}</p> 
         <p>{{dealer.S_Services}}</p>
    </div>   
</div>

listing page
<script>
listing controller
.controller('ListingCtrl', [
'$scope', '$http', '$location', '$window', '$filter','$ionicPopover','$ionicLoading',
function($scope, $http, $location, $window, $filter, $ionicPopover, $ionicLoading) {
$scope.dealers = [{
        S_Date_add: "2015-07-24T13:45:23.927Z",
        S_Email_id: "aditiya@gmail.com",
        S_Store: "samsung",
        Store_Name: "Adtiya Samsung Store",
        S_Services: "Regular Service,Software Faults,Hardware Faults,Interior faults",
        Store_long_description: "Undertake all kind of samsung mobiles",
        Store_short_description: "Undertake all kind of samsung mobiles",
    }, {
        S_Date_add: "2015-07-24T13:45:23.927Z",
        S_Email_id: "rajs@gmail.com",
        S_Store: "nokia",
        Store_Name: "sri shakthi mobile service",
        S_Services: "Settings Faults,Regular Service,Hardware Faults",
        Store_long_description: "Undertake all kind of nokia mobiles",
        Store_short_description: "Undertake all kind of nokia mobiles",
    }, {
        S_Date_add: "2015-07-24T13:45:23.927Z",
        S_Email_id: "sprtive23@gmail.com",
        S_Store: "nokia,samsung",
        Store_Name: "sun mobile service center",
        S_Services: "overall faults,Regular Service,",
        Store_long_description: "Undertake all kind of nokia,samsung mobiles",
        Store_short_description: "Undertake all kind of nokia,samsung mobiles"
    }, {
        S_Date_add: "2015-07-24T13:45:23.927Z",
        S_Email_id: "siva@gmail.com",
        S_Store: "nokia,samsung",
        Store_Name: "mobile service center",
        S_Services: "overall faults,mobile faults",
        Store_long_description: "Undertake all kind of nokia,samsung mobiles",
        Store_short_description: "Undertake all kind of nokia,samsung mobiles"}
    ]


      var model = $location.search().model;
      var items = $location.search().items;
      console.log(filter);
      //filter value iam getting in console samsung 
      console.log(items);
     //items value getting in console  Regular Service,Hardware Faults
     //I have done some logic here to check 
            $scope.dealers=[];
            for(var I=0;I<data.length;I++){
                var temp=data[I].S_Store.split(',');
                 var temp1=data[I].S_Services.split(',');
                for(var j=0;j<temp.length:j++){

                     for(var k=0;j<temp1.length:k++)
                     {
                     if(temp[j]===model ||temp1 === items)
                       $scope.dealers.push(data[I]);
                     }

                    }
                }


}])
</script>

上面的代码列表页面 html 和控制器。在此页面中,我需要根据用户选择显示商店。在列表页面中,我正在获取模型值和项目值。

店铺列表应为:

  1. 模型应与 S_Store 匹配
  2. 项目应与 S_Services 匹配

    我做了逻辑,但它不起作用

示例条件 如果用户来自三星页面,那么我将模型值附加为三星 并且用户选择了常规服务,硬件故障。在这里我像这样重定向 $location.path('listing').search({model: 'samsung', items:'常规服务,硬件故障'}); 所以我在控制台中检查了选定的值,它即将到来 但我需要显示匹配的商店列表 1. Adtiya三星商店 2. sri shakthi 移动服务 3.太阳移动服务中心

【问题讨论】:

    标签: javascript jquery angularjs


    【解决方案1】:

    嗯,在阅读这些内容时会想到一些事情。在您的第一个代码块中,您正在遍历 service 数组,它有四个元素,尽管 iconsservices 数组只有三个,因此会显示意外信息。

    在第二段中,您将遍历尚未定义的 data 变量。此外,在此之前,您将 $scope.dealers 对象重新初始化为一个空数组并清除您之前在页面上设置的信息,因此这可能不是预期的。

    看看解决这两个问题是否会产生你想要的结果。

    编辑:这里有一些代码可能会在下半年对您的评论有所帮助。

    var result = [];
    for(var a = 0; a < $scope.dealers.length; a++) {
        var storeSpl = $scope.dealers[a].S_Store.split(',');
        //Iterate through the store for matches
        for(var b = 0; b < storeSpl.length; b++) {
            if (storeSpl[b] === model)
                result.push(storeSpl[b]);
        }
    
        var servicesSpl = $scope.dealers[a].S_Services.split(',');
        var itemsSpl = items.split(','); //I'm assuming this is serialized as an array - I haven't personally used $location before
        //Iterate through the services for matches
        for(var b = 0; b < servicesSpl.length; b++) {
            for(var c = 0; c < itemsSpl.length; c++) {
                if(servicesSpl[b] === itemsSpl[c]) 
                    result.push(servicesSpl[b]);
            }
        }
    }
    

    【讨论】:

    • 我知道我的逻辑是错误的。在经销商中我需要检查三星(S_Store)、常规服务、硬件故障(S_Services 匹配的位置。我需要根据匹配的条件显示
    • 我的意思是,您正在覆盖我假设您在第二部分中尝试从中读取的数组,因此您将没有任何数据可以迭代。阅读我的更新答案。
    • 我现在该怎么办?帮我前进
    • 再次,请阅读我在回答中为您提供的示例。除非我知道您在哪里遇到困难,否则我无法帮助您。
    • $scope.dealers=[]; for(var I=0;I
    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2011-02-22
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多