0

I have made a directive which bind a click event with the element. On click I want to use a factory which makes an ajax call. How do I achieve this ? My code looks like this

    angular.module('user')
        .directive('activityFeed', function(userFactory, $http) {
        return {
            restrict: 'A',
            link: function($scope, element, attrs) {
                userFactory.getActivities();
                element.bind('click', function() {
                    console.log("hello");
                    userFactory.getActivities();

                })
            }
        }
    })

First call which is outside the bind function works. Now when I click on the attribute it logs "hello" but it doesn't call getActivities().

aditya
  • 996
  • 2
  • 12
  • 25

1 Answers1

2
element.bind('click', function() {
    console.log("hello");
    $scope.$apply(function(){
        userFactory.getActivities();
    });
});
AlwaysALearner
  • 43,759
  • 9
  • 96
  • 78
  • Hey thanks. It worked. Can you tell me why do I need to update the bindings before using factory ? – aditya Sep 16 '13 at 07:44
  • Angular is unaware of the things you do inside your click handler. So I used `$scope.$apply()` to notify angular of the call to be made to the service. – AlwaysALearner Sep 16 '13 at 07:46