Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this is pretty tricky if you don't want to change your DOM structure. If you could just place your <code>contextmenu</code> directive on a sub DOM node inside the <code>&lt;li&gt;</code> things would be a lot easier.</p> <p>However, let's assume you can't do that and let's also assume that you don't own the <code>contextmenu</code> directive so that you can't change it to your needs.</p> <p>Here is a possible solution to your problem that might be a bit hackish (actually I don't know!)</p> <pre><code>'use strict'; angular.module('myApp', []) .controller('TestController', ['$scope', function($scope) { $scope.items = [ {name:1, hasMenu: true}, {name:2, hasMenu: false }, {name:3, hasMenu: true} ]; }]) .directive('contextmenu', function(){ return { restrict: 'A', link: function(scope, element){ element.css('color', 'red'); } } }) .directive('applyMenu', ['$compile', function($compile){ return { restrict: 'A', link: function(scope, element){ if (scope.item.hasMenu){ //add the contextmenu directive to the element element.attr('contextmenu', ''); //we need to remove this attr //otherwise we would get into an infinite loop element.removeAttr('apply-menu'); //we also need to remove the ng-repeat to not let the ng-repeat //directive come between us. //However as we don't know the side effects of //completely removing it, we add it back after //the compile process is done. var ngRepeat = element.attr('ng-repeat'); element.removeAttr('ng-repeat'); var enhanced = $compile(element[0])(scope); element.html(enhanced); element.attr('ng-repeat', ngRepeat); } } } }]); </code></pre> <p>I faked the <code>contextmenu</code> directive to just change the <code>color</code> to <code>red</code> just so that we can see it's taking place.</p> <p>Then I created an <code>apply-menu</code> attribute directive. This directive than checks if the <code>hasMenu</code> property is true and if so hooks in and adds the <code>contextmenu</code> directive and does a manual <code>$compile</code> process.</p> <p>However, what worries me a bit about this solution is that I had to temporally remove the <code>ng-repeat</code> directive (and also the <code>apply-menu</code> directive) to get the <code>$compile</code> process to act the way we want it to act. We then add the <code>ng-repeat</code> directive back once the <code>$compile</code> has been made. That is because we don't know the side effects of removing it entirely from the resulting html. This might be perfectly valid to do, but it feels a bit arkward to me.</p> <p>Here is the plunker: <a href="http://plnkr.co/edit/KrygjX" rel="nofollow">http://plnkr.co/edit/KrygjX</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload