1

I have a code as follow:

<input type="text" ng-model="filteredText">
<ul>
    <li ng-repeat="item in data | filter: {Name : filteredText}">

    </li>
</ul>

When Name property is static it's work correctly. Now I need filter dynamic field. For example:

<ul>
    <li ng-repeat="item in data | filter: {propertyName: filteredText}">

    </li>
</ul>

In this case propertyName is dynamic and come from $scope. But this case filter not working.

0

2 Answers 2

1

After reading the documentation, it looks like you have to do it another way.

<input ng-show="propertyFilter == 'Name'" 
       type="text"
       ng-model="filteredText[propertyName]" />

<ul>
    <li ng-repeat="item in data | filter: filteredText">

    </li>
</ul>
Sign up to request clarification or add additional context in comments.

2 Comments

But I don't know count of propertyName, propertyName come from controller dynamically and I must put it filter syntax
@vaqifrv that way it's quite easier. I updated my answer
1

You could use something like this: fiddle

<div ng-controller="MyCtrl">
   <div ng-repeat="line in lines | filter:custom()">
      <p>
      {{line.Name}}
      </p>
    </div>
</div>

var myApp = angular.module('myApp', [])
function MyCtrl($scope) {
    $scope.filteredText = 'tiago';
    $scope.propertyName = 'Name'

    $scope.custom = function(){
      var object = {};
      object[$scope.propertyName] = $scope.filteredText;

        return object;
    }

    $scope.lines = [
        {Name: '23', age: 20},
      {Name: 'tiago', age: 23}
    ];
}

Comments

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.