0

Below code in controller and a function getSug(). all this code is inside a angular controller. onclick event doesn't invoke getSug() function. How do i invoke it?

var vm = this;
var quant ="linkClick";
vm.link = '<a onclick="'+vm.getSug();+'">' + quant + '</a>';

function getSug(){}

html:  <table>
            <tr>
              <th>link</th>
              
            </tr>
            <tr>
              <td ng-bind-html="vm.link"></td>
             
            </tr>
          </table>
2
  • Why do you need to build the anchor element in this way, as @Kinglish mention in AngularJs you should be using ng-click. Commented May 28, 2021 at 20:07
  • only because these links are built using a map and there are multiple links formed dynamically in controller code using the result of an API. i didn't show here to keep it simple Commented Jun 2, 2021 at 14:27

2 Answers 2

1

In angularjs, onclick is ng-click

Also you wouldn't write your html that way in angular. Rather it would be:

<table>
 <tr>
  <th>link</th>
 </tr>
 <tr>
  <td>
    <a ng-if='quant' ng-click="getSug()" ng-bind-html="quant"></a> 
  </td>
 </tr>
</table>

Then your function would live in the controller that was associated with that page:

$scope.getSug = function() {
   // code
}
Sign up to request clarification or add additional context in comments.

1 Comment

i need to invoke it through controller code in the way i declared variable, not this way.
0

The function getSug gets called on the 3rd line.

Instead of onclick function you just invoke it at the definition line.

Use

var vm = this;
var quant ="linkClick";
vm.link = '<a onclick="'+"vm.getSug()"+'">' + quant + '</a>';

function getSug(){}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.