1

I have two object. //First for items

{
 catId : [1, 2, 3],
 name : "My name is George"
},
{
 catId : 1,
 name : "My name is Bob"
},
{
 catId : 2,
 name : "My name is Trump"
}

And for catId json

{
 Id : 1,
 name : "Hi"
},
{
 Id : 3,
 name : "world"
}

I want when user click in category json after ng-repeat and get category id after that filter my items object with category.Id.

https://plnkr.co/edit/x2O3Q4AA33xwbeqFupAM?p=preview

2

1 Answer 1

0

Try like this

// Code goes here

var app = angular.module('app', []);

app.controller('myController', function($scope){
  $scope.items = [
          {
            Name: "My name is George",
            catId: [ 1, 3, 7 ]
          },
          {
            Name: "My name is Bob",
            catId: 1
          },
          {
            Name: "My name is Trump",
            catId: 2
          }];

  $scope.cats = [
          {
            Name: "Hello",
            Id: 1
          },
          {
            Name: "World",
            Id: 3
          }];
          
          $scope.setCatFilter = function(id){
           $scope.catFilter = id; 
          }
          
          
})
/* Styles go here */

span {
  width: 20px;
  display: inline-block;
  cursor: pointer;
  padding: 5px;
  text-align: center;
  margin-right: 5px;
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myController">
    <div>Filter items</div>
    <span ng-repeat="cat in cats" ng-click="setCatFilter(cat.Id)">{{cat.Id}}</span>
    <div>Category</div>
    <input type="text" ng-model = "serachName">
    <div ng-repeat="item in items | filter:{catId:catFilter,Name:serachName}">
      {{item.Name}}
    </div>
  </div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank dude, also i have search with name filter by with your code its not working. filter:{catId:catFilter}:searchWithName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.