29

I was trying to see if there is a way to call a function I designed inside the scope:

<ul class="ui-listview ui-radiobutton" ng-repeat="meter in meters">
    <li class = "ui-divider">
        {{meter.DESCRIPTION}}
        {{htmlgeneration}}
    </li>
</ul>

$scope.htmlgeneration = function()
{
    ...
}

The function is called htmlgeneration. Essentially, what I want to do is dynamically append HTML inside the li element while using AngularJS.

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
Derek Parker
  • 541
  • 2
  • 5
  • 15
  • While calling a function from HTmL like @SomeKittens said , Your function will be called more than once . Why ? Answer is [Here](https://stackoverflow.com/questions/17164230/angular-scope-function-executed-multiple-times) . So be careful when calling function from HTML . It may slowdown the loading of your page . – Md. Shohag Mia Feb 07 '19 at 08:45

2 Answers2

62

Yep, just add parenthesis (calling the function). Make sure the function is in scope and actually returns something.

<ul class="ui-listview ui-radiobutton" ng-repeat="meter in meters">
  <li class = "ui-divider">
    {{ meter.DESCRIPTION }}
    {{ htmlgeneration() }}
  </li>
</ul>
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
0

I guess my problem was related to conflicts with Django tags. This post was helpful.

What worked for me was a simple solution involving using ng-bind and changing the code to something like this:

<ul class="ui-listview ui-radiobutton" ng-repeat="meter in meters">
  <li class="ui-divider" ng-bind="htmlgeneration(meter.DESCRIPTION)">
  </li>
</ul>
Akaisteph7
  • 5,034
  • 2
  • 20
  • 43