0

I am using jQuery to add an ng-click event to an element using

    $compile($('.selector'))($scope);

after inserting the ng-click attr (which works). However, I would like to be able to wrap this in a function and call it from a service.

Is this possible? I know $scope should not be used within a service so I am unsure as to how I would make this reusable across my application!

Any thoughts would be much appreciated!

Service

app.factory('testService', function() {
   return {
      switchTabs: function() {
      //if condition is met switch tabs using jquery
       $('.selector').attr('ng-click', 'doSomething()');
       $compile($(.loginText))($scope);
       }
   }
});
user2085143
  • 4,162
  • 7
  • 39
  • 68
  • 2
    Please show more of your service. Using jQuery in a service doesn't generally make much sense and ***if it is even needed*** is usually used in a directive. – charlietfl Oct 23 '15 at 11:48
  • Added an example to show what i'm trying to achieve. – user2085143 Oct 23 '15 at 13:08
  • Approach is all wrong to be using jQuery for this. Take jQuery.js out of your page and start using angular best practices. See: http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background – charlietfl Oct 23 '15 at 13:13
  • I thought that might be the case! Thanks for that, better to know now then later. – user2085143 Oct 23 '15 at 14:15

0 Answers0