如果您願意,您可以爲此創建自定義指令。
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