2

I need to add dynamic template on click for that i have used :

var action_template='';
app.controller('EventCtrl',function($rootScope){
     action_template =  action_template +'<ul><li><a ui-sref="/root/authenticate"><img src="default.png"></a></li></ul>';                  
   $rootScope.template=action_template;  
});

In html:i have used ngSanitize

<ul>
       <li ng-bind-html="template"></li> 
</ul>

template is appending perfectly but there is no href link.

what I am doing wrong ? Or is there any solution for doing the same thing ?

The main problem was unable to add any angular directives such as ng-click, ng-href, ui-sref etc. But after adding $sce.trustAsHtml it is adding ui-sref but not adding any hyperlink.

Hint will be appreciable.

Thanks

2
  • Is there a reason you're using $rootScope instead of $scope? Commented Aug 1, 2017 at 11:33
  • Try $rootScope.template=$sce.trustAsHtml(action_template);. Don't forget to inject $sce in your controller Commented Aug 1, 2017 at 11:34

2 Answers 2

2

I think you forgot trustAsHtml in your controller(you can see in the snippet after running, that ui-sref is present in the source):-

var app = angular.module("myApp", []);
var action_template='';
app.controller("myCtrl", function($scope,$rootScope,$sce) {
  
   action_template =  action_template +'<ul><li><a ui-sref="/root/authenticate">Link</a></li></ul>';                  
   $rootScope.template=$sce.trustAsHtml(action_template);  
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
  
<ul>
       <li ng-bind-html="template"></li> 
</ul>
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

You need trust your HTML, using $sce, Try below code

var jimApp = angular.module("mainApp",  []);
jimApp.controller('EventCtrl',function($rootScope, $sce, $scope){
     var action_template='';
     action_template =  action_template +'<ul><li><a ui-sref="/root/authenticate"><img src="default.png" alt="Default image">Link</a></li></ul>';                  
   $scope.template=$sce.trustAsHtml(action_template);  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="mainApp" ng-controller="EventCtrl">
<ul>
       <li ng-bind-html="template"></li> 
</ul>
</div>

4 Comments

now , ui-sref is present but it is not adding any href link , why so ? please see my updated question.
@user7390851 i didn't understand what you mean
after adding $sce.trustAsHtml it is giving like this : <a ui-sref="/root/authenticate"></a> but it should be like <a ui-sref="/root/authenticate" href="/root/authenticate"></a> means auto href will be added automatically.
It's not a problem , angular automatically adding the href when you use ui-sref

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.