1

Here I am trying to show data on rightClick . I created a directive for that. but data was not bind correctly it shows like {{data}} . How can overcome this

jsFiddle

(function() {
   var app = angular.module('right', []);
   app.controller('rightController', function($scope) {
$scope.template = "<div id='ibacontextmenu' ng-click='click()' temp    ngTransclude></div>";
$scope.name = 'ajith';
$scope.desc = 'Hello';
$scope.click = function() {
  alert('clicked');
}
  });
       app.directive('ibaRightMouseDown', function($compile) {

ibaRightMouseDown = {};
ibaRightMouseDown.restrict= 'AE';

 ibaRightMouseDown.ngModel={};
ibaRightMouseDown.link= function(scope, elem, attr) {
//scope.desc=ngModel;
  elem.on('contextmenu', function(e) {
    e.preventDefault();
    //scope.desc=scope.ibaRightMouseDown;

    //scope.desc=scope.ngModel;
    debugger;
    scope.$apply();
   elem.append($compile("<div id='ibacontextmenu' ng-click='click()' temp></div>")(scope));
    $("#ibacontextmenu").css("left", e.clientX);
    $("#ibacontextmenu").css("top", e.clientY);
  });
  elem.on("mouseleave", function(e) {

    $("#ibacontextmenu").remove();

    debugger;
  });
};
return ibaRightMouseDown;
  });
  app.directive('temp',function(){
  return{
 restrict:'A',
  transclue:true,
  template:'<div >{{desc}}</div>'
  };
  });
})();

1 Answer 1

1

I made some changes to your fiddle:

  • Removed ngTransclude it doesn't seem to do anything anyway. By the way it should be ng-transclude when used as an attribute.

  • On the contextmenu event added the line $("#ibacontextmenu").remove(); as the first statement to remove existing context menus.

  • Instead of compiling that hard coded string, I passed scope.template into the $compile service.

  • Did $scope.$apply after appending the compiled DOM elements.

JSFiddle available here.

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

Comments

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.