簡體   English   中英

角度的自定義驗證指令

[英]custom validation directive in angular

我想為我的角度應用程序創建自定義驗證指令。 唯一的問題是我不知道如何獲得價值

<select class="form-control" ng-model="$parent.newSector" ng-options="item.id as item.name for item in sectors" not-empty-array="merchant.sectors"></select>

正如您從我的標記中看到的那樣,我有一個名為notEmptyArray的指令,其中設置了一個表達式(它與ng-model相同,只是一個不同的表達式)。 現在如何在指令中訪問它?

directive = function({
  require: "ngModel",
  link: function(scope, element, attributes, control) {
    //how to access the value of merchant.sectors (its an array in the scope)
  }
});

您需要隔離范圍:

app.directive("notEmptyArray",function(){
    return {
        scope:{
            items:"=notEmptyArray"
        },
        link: function(scope, element, attributes, control) {
            /* anything you feed to "not-empty-array" would be accessible 
              inside items */
            console.log(scope.items);    
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM