【发布时间】:2018-01-11 22:01:32
【问题描述】:
我正在使用此功能从我的控制器发送回 html:
$scope.filterLocation=function(obj){
var loc = $filter('filter')( $scope.locationss, {'product_code':obj});
var htmlstring = "";
angular.forEach(loc, function(value, key) {
htmlstring = '<div ng-click="locationModal()">' + htmlstring + value.location_description + '</div>';
})
return $sce.trustAsHtml(htmlstring);
}
然后我有这个 html 来显示位置列表:
<td><span ng-bind-html="filterLocation(l.productcode)" style="cursor: pointer;"></span></td>
问题是,正如您在控制器中看到的那样,我正在注入 '<div ng-click="locationModal()">',当我查看检查器时,该功能是正确的:
<span ng-bind-html="filterLocation(l.productcode)" style="cursor: pointer;" class="ng-binding"><div ng-click="locationModal()">A5AL</div></span>
但是当我尝试单击它时,我没有得到任何结果,是因为它在 ng-bind-html 内吗?它是一个 div,但我将它更改为一个跨度,看看是否有任何改变。
【问题讨论】:
-
$compile将使用scope将字符串转换为DOM(因此可以使用ng-click) -
使用它只是返回函数虽然我确实尝试使用 $compile,但无法让它工作
-
您应该使用
$compile编写一个指令,然后将其放置在您的<span>上,并使用$sce.trustAsHtml。这是一个有效的plunker 示例(或这个neat version with filters too) -
我不能那样做,因为它在一个 ng-repeat 中,设置一个 $scope.html 不能作为这个选项......它需要在一个返回的函数中
标签: javascript html angularjs