0

I have a directive which creates dynamic datas.I try to run context menu when right click it should go to controller function to run but i can't run function because it can't find controller function from directive.

My Controller:

myapp.controller("myController", ['$scope',
function ($scope)
{
  $scope.test=function(){return $scope.Options = [
        ['test']
        ];
        }
   ........

My Directive:

 myapp.directive('mydirective', [
    '$timeout', function ($timeout) {
        return {

            restrict: 'E',
            template: "<a ng-context-menu='test()'>Click Right</a>",
        .................

How can i call the controller function from dynamically created data?

5
  • Set the controller field on your directive to myController. Commented Apr 30, 2015 at 10:51
  • @user4773604 AngularJS documentation – docs.angularjs.org/guide/… Commented Apr 30, 2015 at 10:54
  • It works but I have 3 different controller ,i can't set a specific controller name ,it should be dynamic Commented Apr 30, 2015 at 10:58
  • I would argue that this is indicative of bad design. Each directive should be a well defined component - having the possibility of 3 mutations does not seem well defined to me. I did check to see if there was a controllerFactory option but there does not seem to be one. If you absolutely must do this, you must inject $controller into a function on the controller field and return a controller that way. But please, do not do this. Commented Apr 30, 2015 at 11:05
  • Is it possible such as; template: "<a mycontroller ng-context-menu='test()'>Click Right</a>" Commented Apr 30, 2015 at 11:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.