2015-08-18 52 views
1

我是AngularJS的新手,仍在學習。自定義指令,需要一些條件之前踢入所需的領域

我想創建一個自定義指令,我想有一些條件,需要一些條件之前,自定義必需的踢球。例如複選框被打勾。以下是我的示例指令和HTML。非常感謝您的回覆。請讓我知道你是否仍然需要一些信息。

.directive('customrequired', function() { 
    return { 
     require: '?ngModel', 
     link: function (scope, elm, attr, ctrl) { 
      if (!ctrl) return; 

      //conditional approach like, requires checkbox to be checked.? 

     } 
    }; 
}) 

HTML

<input type="text" ng-model="Create.ProductName" name="ProductName" customrequired="Create.IsRequired == 1"/> 
<input type="checbox" ng-model="Create.IsRequired" name="Required" /> 
+1

如果它只是簡單的條件,如複選框,那麼它使用ngRequired指令是有意義的。 – dfsq

+0

你介意解釋一下情況嗎? –

+0

@PankajParkar,條件是。如果複選框被選中,它將在您提交表單或焦點不足時觸發驗證。 – Chris

回答

0

你可以做把你的指導價值的手錶,並把你的邏輯,只有當它的真實。

.directive('customrequired', function() { 
    return { 
     require: '?ngModel', 
     link: function (scope, elm, attr, ctrl) { 

       scope.$watch(attr.customrequired,function(value){ 
       if(value){ 
       //conditional approach like, requires checkbox to be checked.? 
       } 
      }) 

     } 
    }; 
}) 
+0

嗨@sjjt,你能否給我一個關於條件區域的例子邏輯?不知道在 – Chris

+0

裏面if語句你可以將事件監聽器附加到onblur的元素上,然後在那裏執行你的驗證。如果值不是true,則刪除偵聽器。 – srjt