Javascript AngularJS指令,模板具有ng repeat和ng bind html
我已经创建了一个Javascript AngularJS指令,模板具有ng repeat和ng bind html,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我已经创建了一个指令,模板有ng repeat和ng bind html,如下所示 app.directive('customField', function ($compile) { var getTemplate = function (customType) { return '<span ng-repeat ="option in custom.options" ng-bind-html="option"></span>'; } var linker
指令
,模板有ng repeat
和ng bind html
,如下所示
app.directive('customField', function ($compile) {
var getTemplate = function (customType) {
return '<span ng-repeat ="option in custom.options" ng-bind-html="option"></span>';
}
var linker = function (scope, element, attrs) {
element.html(getTemplate(scope.custom.type)).show();
$compile(element.contents())(scope);
}
return {
restrict: "E",
rep1ace: true,
link: linker,
scope: {
custom: '='
}
};
});
app.directive('customField',函数($compile){
var getTemplate=函数(customType){
返回“”;
}
变量链接器=函数(范围、元素、属性){
html(getTemplate(scope.custom.type)).show();
$compile(element.contents())(范围);
}
返回{
限制:“E”,
代表1:是的,
链接:链接器,
范围:{
自定义:'='
}
};
});
但这会产生错误:
错误:[$sce:unsafe]试图在安全上下文中使用不安全值
在正常情况下,我使用
$sce.trustAsHtml(html\u代码)
来解决此类问题,但在指令中使用ng repeat
如何解决此问题?将$sce.trustAsHtml()绑定为作用域函数并调用它?或使用ngSanitize
!谢谢你的建议。现在它开始工作了。谁的建议?碧溪的还是我的?:)两种解决方案都很好,但我使用了Bixi,因为我不想再添加一个js'angular sanitize.js'D