1

I am trying to add dynamically created anchor tag in HTML for this purpose I
have used below code in JS :

var tabList = $sce.trustAsHtml('<a ng-click="getActiveTag()">Test</a>');
var temp = $compile(tabList)($scope); 
$scope.tabList = temp[0];

and on html side I have used :

<p ng-bind-html="tabList"></p>

but ng-click not working :( ..any help would be appreciated.

3
  • can you give us a plunker/fiddle ? Commented Oct 11, 2015 at 6:44
  • 1
    I think you could use a directive instead of ng-bind-html Commented Oct 11, 2015 at 6:50
  • 1
    you have to manually insert that $compiled dom, not with ng-bind-html. Commented Oct 11, 2015 at 7:07

3 Answers 3

2

You can use .appendTo function. Adding sample code.

<body>
<p id="appendHere"></p>
</body>

.js
angular.module("testApp",[]).controller("testContoller", function($scope, $compile)
{
$scope.tabList = '<a ng-click="getActiveTag()">Test</a>';
$compile($scope.tabList)($scope).appendTo(angular.element("#appendHere")); 
//$scope.tabList = temp[0];
});

$scope.getActiveTag = function()
{
    alert('calling anchor click');
}

This needs jquery loaded first before angular library.

Edited

var testVar = angular.element('<a ng-click="getActiveTag()">Test</a>');
$compile(testVar)($scope).appendTo(angular.element("#appendHere")); 
Sign up to request clarification or add additional context in comments.

Comments

1

Thank you all for your suggestions .I found the solution, I have removed ng-bind-html and place already compiled DOM as suggested by "YOU".And after studying thoroughly Angularjs-compiler, follow the steps below and it works :)

// Step 1: parse HTML into DOM element
var template = angular.element(''<a ng-click="getActiveTag()">Test</a>'');
// Step 2: compile the template
var linkFn = $compile(template);
// Step 3: link the compiled template with the scope.
var element = linkFn($scope);
// Step 4: Append to DOM (optional)
$("#content").append(element);

3 Comments

This is exactly what i mentioned in my answer dude...In this solution you are extending number of coding line which I believe you should look into it.. Anyway happy coding...!!! :)
Thanks Akash, I have tried your solution only thing changed is first parse HTML into DOM element and then pass this dom element to $compile "$compile(template) " instead of passing directly $scope element "$compile($scope.tabList)".
Dude..I dont know what you are referring to..If you are concerning $scope element then I would say you can still declare it as a normal variable.. See my edition. Apart from this you have static anchor element which you need to compile and for that I dont think you need to parse that HTML into DOM element with angular.element function.
0

Try ngHref in your HTML instead:

<p>
  <a ng-href="getActiveTag()">Test</a>
</p>

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.