Pregunta

Basically, I'm trying to use $watch with an angularFireCollection. It doesn't appear to be working however the way I want it to.

This is what I have at the moment:

$scope.bits = angularFireCollection(new Firebase(url).limit(75));

$scope.$watch('bits', function() {

    console.log('new');

});

So, when I load the page it logs "new" in the console when the data is first loaded, and that's fine, but when new data comes in, it doesn't log anything. Any idea as to why this happens?

¿Fue útil?

Solución

$watch only looks at the object references (it compares them using ===), so since $scope.bits is always the same array instance, even though its contents have changed, it doesn't fire your listener.

You can pass an additional parameter to $watch that makes it do comparison of the contents of the objects:

$scope.$watch('bits', function() {}, true);

I'd never heard of using bits.length as @robertklep suggested, but that also looks elegant and I'd upvote it if he posted it as an answer.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top