javascript - Custom filter in Angular -
i have problem custom filter. can't include it's in project. firstly used filter: text. understand array initialized asynchronously , used custom filter. when include filter, have problem ($injector:unpr). sorry english.
<div ng-controller="productlist products"> <form ng-submit="addproduct()"> <input type="text" placeholder="enter product" ng-model="producttitle"> <button>add</button> </form> <ul class="productlist"> <li ng-repeat="product in list track $index | custom:search"> {{product.title}} </li> </ul> <input type="text" placeholder="search" ng-model="search"> </div>
app here
angular.module('lists', []) .controller('productlist', function($scope) { $scope.list = []; $scope.addproduct = function() { $scope.list.push({title:$scope.producttitle}); $scope.producttitle = ""; } });
and filter is
.filter('custom', function() { return function(input, search) { if (!input) return input; if (!search) return input; var expected = ('' + search).tolowercase(); var result = {}; angular.foreach(input, function(value, key) { var actual = ('' + value).tolowercase(); if (actual.indexof(expected) !== -1) { result[key] = value; } }); return result; } });
i checked code , issue seems place filter being applied.
fiddle: http://jsfiddle.net/pfgkna8k/2/. though didn't know implementing.
please check code
<div ng-app="lists"> <div ng-controller="productlist products"> <form ng-submit="addproduct()"> <input type="text" placeholder="enter product" ng-model="producttitle" /> <button>add</button> </form> <ul class="productlist"> <li ng-repeat="product in list track $index" ng-bind="filteredtitle = (product.title | custom: searchbox)" ng-show="filteredtitle"> </li> </ul> <input type="text" placeholder="search" ng-model="searchbox" /> </div> </div>
Comments
Post a Comment