如何在动态HTML模板中添加UI-sref?



我需要在单击时添加动态模板,因为我使用了:

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;  
});

在html:i中使用了ngSanitize

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

模板完美附加,但没有 HREF 链接。

我做错了什么?或者有什么解决方案可以做同样的事情吗?

主要问题是无法添加任何角度指令,例如ng-click,ng-href,ui-sref等。 但是在添加$sce.trustAsHtml之后,它添加了ui-sref,但没有添加任何超链接。

提示将是可观的。

谢谢

我想你忘记了控制器中的trustAsHtml(你可以在运行后的片段中看到,ui-sref存在于源代码中(:-

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>

你需要信任你的HTML,使用$sce,试试下面的代码

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>

最新更新