Dear all I was going through a question where the accepted answers suggested to use a filter with ng-repeat which basically use lodash's chunk function along with memoize function. The author describe the reason for using memoize is following
Many people prefer to accomplish this in the view with a filter. This is possible, but should only be used for display purposes! If you add inputs within this filtered view, it will cause problems that can be solved, but are not pretty or reliable.
The problem with this filter is that it returns new nested arrays each time.Angular is watching the return value from the filter.
The first time the filter runs, Angular knows the value, then runs it again to ensure it is done changing. If both values are the same, the cycle is ended.
If not, the filter will fire again and again until they are the same, or Angular realizes and infinite digest loop is occurring and shuts down.
Because new nested arrays/objects were not previously tracked by Angular, it always sees the return value as different from the previous.
To fix these "unstable" filters, you must wrap the filter in a
memoize
function.
lodash
has amemoize
function and the latest version oflodash
also includes achunk
function, so we can create this filter very simply usingnpm
modules and compiling the script withbrowserify
orwebpack
.
Can anyone please help me understand how memoize is functioning here with ng-repeat and filter?