2017-03-06 5 views
0

である私のカスタムディレクティブである:ここで値はここで常に未定義

angular 
    .module('accountApp') 
    .directive('uniqueRecord', function($q, $timeout, $http) { 
     return { 
      require: 'ngModel', 
      link: function(scope, elm, attrs, ctrl) { 

       ctrl.$asyncValidators.uniqueRecord = function(modelValue, viewValue) { 

        var value = modelValue || viewValue; 

        var attributes = scope.$eval(attrs.uniqueRecord); 

        // Lookup effect by name 
        return $http.get(attributes.url + '/' + value + '/' + ((attributes.currentRecordName == '' || attributes.currentRecordName == 'nothing') ? '_' : attributes.currentRecordName)) 
           .then(function resolved() { 
            //username exists, this means validation fails 
            return $q.reject('exists'); 
           }, function rejected() { 
            //username does not exist, therefore this validation passes 
            return true; 
           }); 
       }; 
      } 
     } 
    }); 

はHTMLです:

<input type="text" id="name" name="name" class="form-control form-input" ng-model="effect.name" 
     ng-disabled="isReadOnly" required 
     unique-record="{ url: '/api/effect', currentRecordName: {{currentEffectName == '' ? 'nothing' : currentEffectName}} }" 
     ng-uniqueRecord-err-type="duplicateRecord"/> 

あなたは上記のHTMLで見ることができるように、私は値を渡していますcurrentRecordNameをディレクティブに追加します。ディレクティブでは、urlの値はそのまま渡されますが、currentRecordNameの値は常に未定義です。どうして?

答えて

1

問題は、評価された表現の口ひげを利用することに起因します。 評価された式は既に角式とみなされており、口ひげは必要ありません。

私はあなたにあなたのHTMLを変更することをお勧め:

<input type="text" id="name" name="name" class="form-control form-input" ng-model="effect.name" 
    ng-disabled="isReadOnly" required 
    unique-record="{ url: '/api/effect', currentRecordName: currentEffectName }" 
    ng-uniqueRecord-err-type="duplicateRecord"/> 

直接指示コードの「何もない」VS currentEffectNameを扱います。

var attributes = scope.$eval(attrs.uniqueRecord); 
if (!attributes.currentRecordName) { 
    attributes.currentRecordName = 'nothing'; 
} 
+0

Oups、申し訳ありませんが、私はすでにあなたがそれを解決したとは思わなかった。 –

+0

問題ありません。助けてくれてありがとう。私はupvoteします。 – Vishal

+0

私はあなたの答えをテストし、それは正常に動作します。だから私は私の答えを削除し、あなたのことを受け入れるでしょう。手伝ってくれてありがとう。 – Vishal

関連する問題