Search code examples
javascriptangularjsangularjs-scopeangularjs-ng-include

How to call a method inline in ng-include and concatenate the results?


I would like the results from a method to be part of the string that gets passed to ngInclude. Something like this:

<div ng-repeat="result in queryResults.results">
   <div class="box" ng-include="'views/' + getRenderer(result) + 'result.html'"></div>
</div>

This doesn't work. it doesn't throw an error, but the result is views/result.html. I put logging in getRenderer() and verified it wasn't being called.

I'm not sure why my code above doesn't work while this code DOES work:

<div class="box" ng-include="'views/widgets/' + result.someVariable + 'result.html'"></div>

edit: getRenderer just returns a string:

function getRenderer(result) {
    if (result === 'a') {
        return 'aaa';
    else 
        return 'bbb';
}

Solution

  • You should add below to your controller. Basically provide an pointer of your function to scope variable so that it could be accessible on HTML

    $scope.getRenderer = getRenderer