1

I'm making table with lots of data (using ag-grid - http://www.ag-grid.com/, but it's not important).

In that table I want to trigger angular service to open modal (with detailed info). Problem is that I don't want to use angular's directive (I have over 10000 records) only to use something like <a on-click="angular.service('modal').method('open-modal').params({id:1})">Open modal</a>

Is it possible?

2
  • Is this inside a controller? (that is, inside html which has a ng-controller attribute on the top level) Commented Nov 2, 2015 at 11:58
  • Yes - whole code is in angular element (directive) Commented Nov 2, 2015 at 12:03

2 Answers 2

2

Calling something angular-related outside it is quite a bad practice (it might also affect the digest/apply loop and not work as intended). Maybe there's an alternative:

If you are inside a controller you can save your service inside a scope variable and then use ng-click to call a function inside your service.

var Controller=function($scope, YourService) {
    $scope.myService=YourService;
};
Controller.$inject=['$scope', 'YourService'];

app.controller('Controller', Controller);

And then, in the HTML:

<a ng-click="myService.method('open-modal').params({id:1})">Open modal</a>

if you still need to use angular-related methods outside it then you should be able to call the "scope()" function this way:

<a on-click="angular.element('#idOfTagWithNgController').scope().myService.method('open-modal').params({id:1})">Open modal</a>

You still need to assign your service to a scope variable to be able to access it this way. idOfTagWithNgController is of course the ID of the HTML tag with the ng-controller attribute (if it has no id you can add it). Mind that this is still bad practice and should be avoided

Sign up to request clarification or add additional context in comments.

3 Comments

I can use directive in that element because ag-grid has great option to compile rows with angular angularCompileRows but I have serious preformance issue even though there are no watchers
so the issue is not making a new directive but not being able to use any, not even angular default directives, because of angularCompileRows affecting performance?
Thanks I'll try and give response
1

You can write your own service and register it as a factory, than inject it in your controller and call service method from your controller when triggering ng-click

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.