2012-10-21 3 views
2

특성의 존재 여부을 AngularJS의 변수에 바인딩하고 싶습니다. 구체적으로는 sandbox (iframe)입니다.AngularJS Bind 특성 존재 여부

<iframe src="..." sandbox /> 

sandbox 속성이 때 allowJavascript == false 존재하는 경우, 내가 때 allowJavascript == true 샌드 박스 속성이 사라하려는 : 내가 원하는, 내가 변수로 $myCtrl.allowJavascript을 말해봐.

AngularJS에는이 메커니즘이 있습니까?

가장 가까운 것은 here이며, 기본적으로 "특정 속성에 대해서만 작동합니다"라고되어 있지만 샌드 박스는 아닙니다.

+0

이것은 잠재적으로 위험한 디자인입니다. 샌드 박스 속성은 iframe을 탐색 할 때만 적용됩니다. 다른 말로하면, 이것은 당신이 생각하는 것을하지 않을 수도 있습니다. – user239558

답변

3

같은 것 줄 :

app.directive("addAttr", function() { 
    return function(scope, element, attrs) { 
    scope.$watch(attrs.addAttr, function(addAttr) { 
     for (var key in addAttr) { 
     if (addAttr[key]) element.attr(key, true); 
     else element.removeAttr(key); 
     } 
    } 
    } 
} 

당신은 그것을 좋아 사용하십시오 :

<iframe add-attr="{sandbox: allowJavaScript, someOtherAttr: someOtherVar}"> 
+0

attrs.addAttr에 대한 평가가 끝났지 만 작동합니다. – Chuck

3

원하는 경우 사용자 지정 지시문을 만들 수 있습니다.

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

감사합니다. 괜찮습니다. – Chuck