720kb / angular-tooltips

Angularjs tooltips module, add tooltips to your elements - https://720kb.github.io/angular-tooltips
351 stars 157 forks source link

Is it possible to pass data into the template controller? #201

Closed DvirH closed 7 years ago

DvirH commented 7 years ago

I want to load dynamic data according to an element Id. For that I have to pass the element Id to the template controller, but I can't find a way to do so. Any Ideas?

steczol commented 7 years ago

Hi! Yes, it is possible, since the scope of your controller is a not-isolated child-scope of the linking function`s scope (see newScope = scope.$new(false, scope) in function onTooltipTemplateControllerChange(newValue) .

Here is what I did:

  1. I added new option: tooltip-id="myTooltipID" to html element I want to know ID of
  2. I modified the function getAttributesToAdd(element) by adding: if (element.attr('tooltip-id') !== undefined){ attributesToAdd['tooltip-id'] = element.attr('tooltip-id'); element.removeAttr('tooltip-id'); } now myTooltipID will be stored in $attrs.
  3. Since controller's scope does not inherit $attrs, the variable containing myTooltipID has to be exposed to $scope. Therefore I added following line to linkingFunction(...) : $scope.tooltipID = $attrs.tooltipId;
  4. You should be able now to access the ID in your controller by checking: $scope.$parent.tooltipID;
45kb commented 7 years ago

thanks @steczol for the example.

DvirH commented 7 years ago

@steczol Thanks!