Subhash
Subhash

Reputation: 3

Angular JS: ng-include by function

I am having strange experience while using ng-include. I am trying to get template path by triggering a function defined in controller which is returning path as per parameter passed. Here is my code-

<tr ng-repeat="detail in Ctrl.details" ng-include="Ctrl.getTemplate(object)"></tr>

Contoller-

self.getTemplate = function (obj) {
    if (<condition>) {
        return 'view1';
    } else return 'view2';
};

This is working really fine, but I observed really strange behavior while debugging the code. In my table row I am having 3 buttons and I applied Bootstrap tooltip on them. Whenever I hover them tooltip comes up & on mouse left getTemplate() gets called. Do anybody know why this is happening?

Upvotes: 0

Views: 183

Answers (1)

martin
martin

Reputation: 96969

This is expected behaviour.

Have a look at this article. Angular needs to check whether the expression for ng-include has changed or not. In order to do that it needs to evaluate Ctrl.getTemplate(object) on every digest loop because there's no other way to find out whether its return value has changed and therefore it needs to pass a new value to ng-include.

Upvotes: 1

Related Questions