Result of filtered ng-repeat array not in controller

45 views
Skip to first unread message

Marco

unread,
Apr 21, 2015, 1:43:42 PM4/21/15
to ang...@googlegroups.com
Hello,

i have an ng-repeat with a filter that is linked to a text field to filter the results

<h3>List <span ng-bind="filteredProducts.length"></span></h3>
<input type="text" ng-model="search.name" placeholder="name">
<div data-ng-repeat="product in filteredProducts =(products.productList | psearch:search.name)" class="productList">
//use the product info...
</div>

i have created a filter

.filter('psearch',function(){
   
return function(element,searchname) {
       
console.log("psearch called");
       
console.log("element:",element);
        var out = [];
       
angular.forEach(element, function(product) {

           
if (product.naam.match(searchname)) {
               
out.push(product);
           
}
       
});
       
console.log("out",out); //this gives me an array with the number of items matching the result
       
return out;
    }
});

this filter works and the "filteredProducts.length" in my span in the h3 tag above gets updated with the number of results of the filter.

now i want to use this number in my controller but when i try to access it like

$scope.filteredProducts.length

i get an error cannot get length of undefined 

also when i check this with

console.log("filtered:",typeof $scope.filteredProducts);

i get:

filtered: undefined

how can i fix this?

thank

Marco




Sander Elias

unread,
Apr 22, 2015, 12:39:58 AM4/22/15
to ang...@googlegroups.com

Hi Marco,

You controller runs before your template ‘runs’. add an ng-Change='triggerSomething' to your input. in the triggerSomething you will have the change to do what you want.
Best thing you can do is move the filteredProducts =(products.productList | psearch:search.name) part into that function, and have less logic in your template!

Is that enough for you?

Regards
Sander

Marco

unread,
Apr 24, 2015, 12:59:20 AM4/24/15
to ang...@googlegroups.com
Hello Sander,
Thanks for pushing me in The right direction, now i use my filter in the function called by ngChange and iT works.

At first i tried to put The Logic of my filter in that function but that gave me problems because of The resource object, The filter takes care of this.

Thanks again!

Gr Marco

Santiwijoyo Salim

unread,
Dec 3, 2015, 12:24:55 AM12/3/15
to AngularJS
Hi Marco,

I am trying to do what Marco instructed which is to move the filtering part into a function to have less logic in template with no luck.

Can you provide me with a sample how you manage to do this?

Regards,
Santiwijoyo

Marco Stolle

unread,
Dec 3, 2015, 2:40:51 AM12/3/15
to ang...@googlegroups.com
Hello Santiwijoyo

i first made a filter 

angular.module('timers').filter('filterww',[ 'appdata', function(appdata){
return function(input,search,timerscontroller){
var out = [];
angular.forEach(input, function(timer){
if(search)
            var descriptionLC = timer.description.toLowerCase();
            searchLC = search.toLowerCase();
if(descriptionLC.match(searchLC)){
out.push(timer);
}
});
return out;
};
in my view i put an input with an ng-change attached to it that calls the filterTimers() function located in my controller
<input type="text" ng-model="timersCtrl.appdata.timer.queryww" placeholder="Description" data-ng-change="timersCtrl.filterTimers()">
function in controller:
self.filterTimers = function(){
    var filter = function(){
        self.filteredTimers = $filter('filterww')(self.appdata.timers, self.appdata.timer.queryww, self);
    };
};

I've stripped some irrelevant code from the example, i don't consider myself an expert yet so if anyone finds an error in this way of thinking please let us know.

hope this helps you

Gr

Marco



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/3QGdSHtpyY0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Groeten

Marco

Santiwijoyo Salim

unread,
Dec 3, 2015, 2:50:54 AM12/3/15
to AngularJS
Hi Marco,

Thanks for the quick reply. This is definitely something I could learn and try to understand.

Thanks.

Regards,
Santiwijoyo
Reply all
Reply to author
Forward
0 new messages