1

I was wondering I am able to call a bound function within a directive when part of a div is clicked. I currently have a div with another div inside that serves as a button to expand the div. The large div has a directive that makes it expandable. I would like to click on the arrow in the corner of the div and have the arrow's ng-click call a bound function inside the expandable directive. Is this possible? If so, how can I implement it?

Here's a picture of the div: enter image description here

<div expandable class="boxWithBorderStyle"> <div class="arrowStyle" ng-click="someFuncInsideExpandableDirective()"> </div> </div> 
1
  • can you provide a plunker or fiddle demo Commented Jun 2, 2015 at 13:56

1 Answer 1

1

The contents - i.e. the child elements - of the element hosting the directive have a different scope than the directive if the directive uses isolate scope (scope: {}), which I suspect is what you have.

In order to link the contents against the same scope, you'd need to manually transclude them:

.directive("expandable", function(){ return { transclude: true, scope: { // whatever you have now }, link: function(scope, element, attrs, ctrls, transcludeFn){ transcludeFn(scope, function(contentClone){ element.append(contentClone); }); // here you can expose whatever functions you want on the isolate scope: scope.someFuncInsideExpandableDirective = function(){ // do something } } }; }); 

The first parameter of the transclusion function transcludeFn is the scope against which the transcluded content is linked, which, in this case, is the directive's scope.

Keep in mind, though, that it is a bit awkward from the point of view of the user of your directive, since now they have HTML that refers to some "magic" variables/function (which is of-course defined by your directive) that is not apparent to someone observing the HTML. Imagine you'd encounter some directive foo which did something similar:

 <div foo="abc"> {{foobar}} <button ng-click="doSomethingFoo()">do foo</button> </div> 

and foobar and doSomethingFoo were defined inside the scope of the foo directive - it would be more difficult to decipher without knowing the specifics of foo what was going on.

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

4 Comments

I'm trying to follow your post above: I have changed my link function to 'link : function (scope, element, attrs, expand) ' to include an expand function I would like to call from the html. my directive returns an object where transclude = true. I am having an issue declaring the function I would like to call from the directive. So far I have 'expand(scope, function() {alert( id + " expanded!"); });' and I am encountering the error: "expand is not a function" Do I need to declare my function else where?
Also, how do I copy my content and pass it as a var? Thanks
@hededo, link function has well-defined parameters that it gets called with - I, in fact, outlined what those parameters are - you can learn more about what they are here. Specifically, the 5th parameter is the transclude function. When you invoked the 4th parameter (that you named locally expand), it didn't inject an "expand" function (whatever it may be). In any case, I think I understand your difficulty, so I edited the answer to clarify
@hededo, I'm not sure what "content" you are referring to. If it is not directly related to the original question, then to avoid this being a loaded question, I suggest you ask a new question specific to your new issue, like passing content (whatever you mean by that). this will give you the proper space to explain what you want

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.