angularjs - Angular use $cacheFactory data in controller -
i hava prodcache service used cache products array.
... var products = prodcache.get('all'); if(typeof products == 'undefined'){     products = product.query();     prodcache.put('all',products); } if put products on scope products shown expected, need few products shown.
 try:
$scope.related = (function(){     var res = [];     if(products.length>0){         (var = 0, key; < 3; i++) {             key = math.floor(math.random() * products.length);             res.push(products[key]);         }     }     return res; })(); that function wont work first time because xhr request being processed , returned data not reactive.
the proper way use filters docs here , here.
assuming filter wrote mock, , need complex filter, have create filter function @ $scope , reference @ ng-repeat expression:
$scope.isrelated = function isrelatedfilter(item) {   // if return true, item included.   return item.someproperty === 'somecriteria'; } <span ng-repeat="product in products | filter:isrelated">...</span> the somecriteria $scope/controller property or defined service. if need custom parameters, can't use filter filter , should create own. take @ docs.
Comments
Post a Comment