如果您愿意,您可以为此创建自定义指令。
app.directive('sandboxIf', function() {
return {
restrict: 'A',
link: function(scope, elem, attr, ctrl) {
/* eval whatever was passed to sandbox-if=""
to see if it's true or false. */
if(scope.$eval(attr.sandboxIf)) {
elem.attr('sandbox','sandbox'); //sandbox="sandbox"
}else{
elem.removeAttr('sandbox');
}
}
};
});
使用是这样的:
<iframe sandbox-if="foo"></iframe>
甚至
<iframe sandbox-if="test()"></iframe>
其中控制器会像
app.controller('MyCtrl', function($scope) {
$scope.foo = true;
$scope.test = function() {
return true;
};
});
这是一个潜在的危险设计。只有当iframe被导航到时,沙箱属性才会生效。换句话说,这可能不会做你认为它的作用。 – user239558