2014-12-24 2 views
0

사용자가 필드에 아무 것도 입력하지 않은 경우이 지시문에 의해 생성 된 입력 필드의 클릭에 대한 지시문을 제거하는 방법을 알아 내려고하고 있습니다. 현재 지시문은 아무 것도 입력되지 않는 경우를 제외하고는 계획대로 작동하는 것으로 보입니다. 기본적으로 입력 된 지시문이 제거되지 않고 어떤 함수가 호출 된 경우. 난 그냥 지시문 템플릿을 제거 싶어요. 감사.AngularJS는 clickaway에서 지시문을 제거합니다.

DocumentManager.directive('clickAway', function($compile,$document, $location){ 

return { 
replace: true, 
scope: true, 
template:'<input type="text" class="form-control" ng-model="item.name" placeholder="Enter name">', 
restrict: 'AE', 
link: function(scope, elem, attr, ctrl) { 
    elem.bind('click', function(e) { 

    e.stopPropagation(); 
    }); 
    $document.bind('click', function() { 
    if(scope.item == null){  
     return; 
    }; 
    scope.createItem(scope.item); 
    scope.item= null; }); 
} 
}}); 
+1

이것은 'ng-if'가 사용되는 이유입니다. – cgTag

답변

1

거의 다 왔어. 당신은,하지만 elem.remove();

$document.bind('click', function() { 
    if(scope.item == null){ 
     elem.remove();  
     return; 
    }; 
    scope.createItem(scope.item); 
    scope.item = null; }); 
} 

그냥 통지를 추가 할 필요가 당신이 그것을 구현 된, 경우 item.name = "" (외부 설정), 다음 첫 번째 클릭을하고, 그것을 제거하지 않을 것이다 사실 createItem()인치

관련 문제