Ok, I think I've got this squared away...posting here in case it might help
someone else.
Sander's recommendation to wrap the call to my controllers changeCategory()
method in a "scope.$apply()" definitely needed to be done. However, even
though I could see that my controller's method was being successfully
called, the UI wasn't updating correctly: I'd drag a task from one category
to a new category, but that task wouldn't show up in the new category (or
disappear from the old category).
I think this was because in my controller's method, I was trying to find
the task I was dropping (based on its ID) by using a filter:
var task = $filter('filter')($scope.tasks, {id: task_id}, true);
I remember reading that any time you use a filter on an array, a new copy
is made of that array containing the filtered data. I think by doing this,
the task I was finding (and updating) belonged to a different (new) array
than was being used for my bound task list. So, instead of using a filter
to find my task, I did it the old fashioned way, using a loop:
for(var i=0; i<$scope.tasks.length; i++) {
var task = $scope.tasks[i];
if(task.id == task_id){
task.category_id = category_id;
}
}
This seems to be working perfectly! It seems like there should be a more
elegant way of doing this but it is behaving as expected. Here's a new
bin<http://jsbin.com/cejohana/5/edit?js,output> showing
the result.
If anyone has any ideas regarding a cleaner way of accomplishing this, I'd
be curious to hear about it. This gives me enough to work with, though.
Thanks!
On Friday, April 25, 2014 9:12:34 PM UTC-7, Sander Elias wrote:
>
> Hi Brian,
>
> You are changing things outside angular. You have to tell angular to
> update it's stuff. You do that with an scope.$apply
> have a look: http://jsbin.com/qohunubi/1/edit
>
> Here is an article that explains it a bit more:
> http://jimhoskins.com/2012/12/17/angularjs-and-apply.html
>
> Regards
> Sander
>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.