2017-02-20 28 views
0

我正在學習平均堆棧開發和沿着視頻教程課程。一切都很順利,直到我嘗試匹配兩個密碼字段時遇到此錯誤。 「密碼」和「確認密碼」 我寫的確切代碼教程但我得到的命令行下面的錯誤(咕嘟咕嘟服務)

錯誤「$ ATTRS」不defind NO-民主基金

錯誤 「$ ATTRS」 不defind沒有民主基金

下面的代碼

export function CompareToDirective($parse) { 
'ngInject' 
return { 
    require: 'ngModel', 
    link: function (scope, elm, attrs, ngModel) { 
     var mainModel = $parse(attrs.compareTo); 
     var secondModel = $parse(attrs.ngModel); 

     scope.$watch(attrs.ngModel, function (newValue) { 
      ngModel.$setValidity($attrs.name, newValue === mainModel(scope)); 
     }); 

     scope.$watch(attrs.compareTo, function (newValue) { 
      ngModel.$setValidity($attrs.name, newValue === secondModel(scope)); 
     }); 
    } 
} 

}

編輯

這裏是我試圖驗證,以防萬一

<form name="register"> 
       <div class="form-group"> 
        <label>Email address</label> 
        <input type="email" class="form-control"> 
       </div> 
       <div class="form-group"> 
        <label>Password</label> 
        <input type="password" class="form-control" name="pwd" ng-model="pwd"> 
       </div> 
       <div class="form-group"> 
        <label>Password Confirm</label> 
        <input type="password" class="form-control" compareTo="pwd" name="pwdConfirm" ng-model="pwdConfirm"> 
       </div> 
       <span ng-show="register.pwdConfirm.$invalid">Passwords do not match</span> 
       <button type="submit" class="btn btn-default">Submit</button> 
      </form> 
+0

刪除「$」從$ ATTRS ..只是使用ATTRS –

+0

你確定它不只是'attrs' ...這是你所擁有的在你的函數定義中。 – TripeHound

+0

http://pasteboard.co/AG6q8O6OL.png 以下是教程中代碼的屏幕截圖。其中有「$ attrs」 當我從「attrs」刪除$ $ 錯誤去但仍然表單不起作用 –

回答

0

好形式。我能夠得到它的工作。請從下面的代碼

.directive("compareto", function($parse) { 
    return { 
    require: 'ngModel', 
    link: function (scope, elm, attrs, ngModel) { 
    var mainModel = $parse(attrs.compareto); 
    var secondModel = $parse(attrs.ngModel); 

    scope.$watch(attrs.ngModel, function (newValue) { 
      ngModel.$setValidity(attrs.name, newValue === mainModel(scope)); 
    }); 

    scope.$watch(attrs.compareto, function (newValue) { 
     ngModel.$setValidity(attrs.name, newValue === secondModel(scope)); 
    }); 
} 
} 
}); 

和形式將是

<form name="register"> 
      <div class="form-group"> 
       <label>Email address</label> 
       <input type="email" class="form-control"> 
      </div> 
      <div class="form-group"> 
       <label>Password</label> 
       <input type="password" class="form-control" name="pwd" ng-model="pwd"> 
      </div> 
      <div class="form-group"> 
       <label>Password Confirm</label> 
       <input type="password" class="form-control" compareto="pwd" name="pwdConfirm" ng-model="pwdConfirm"> 
      </div> 
      <span ng-show="register.pwdConfirm.$invalid">Passwords do not match</span> 
      <button type="submit" class="btn btn-default">Submit</button> 
     </form> 
+0

我正在導出函數'CompareToDirective'但現在我似乎不明白怎麼會我把它變成我可以導出的函數。你可以讓它像我可以使用相同的名稱exprort到'index.module.js'中。 對不起,在你的代碼中使用noob –

+0

嘗試改變compareTo =「pwd」來比較-to =「pwd」。 –

+0

感謝@pawan 它讓現在的工作,我代替我這個代碼 '{回報要求 : 'ngModel', 鏈接:功能(範圍,榆樹,ATTRS,ngModel){VAR = mainModel $解析(ATTRS 。相比於); var secondModel = $ parse(attrs.ngModel); 範圍$ watch(attrs.ngModel,function(newValue){0} {0} $ setValidity($ attrs.name,newValue === mainModel(scope)); }); 範圍。$ watch(attrs.compareTo,function(newValue){0} {0} {setValidity($ attrs.name,newValue === secondModel(scope)); }); } }' 給你。 –