2017-05-14 9 views
0

私は同様の質問をチェックしていますが、この状況には該当しません。angularJSエラー:変数が定義されていない場合でも、内部でisDefined()関数

私は手動で検証しようとしている角型のアプリケーションがありますが、これに問題があります。

簡略化のため、電子メールフィールドのみを含めています。フォームフィールドはすでに別の関数で検証されており、その関数は$ scope.regform.email.valid変数をtrueまたはfalseに設定します。

誤った情報を故意にフィールドに入力するとうまく動作しますが、何も入力せずに登録ボタンが押された場合、isDefinedチェックを実行したいと思います。

//this is inside the function that runs is the register button is pressed. 
//in this test case nothing has been entered in the email field when the register button is pressed. 

email = angular.isDefined($scope.regform.email.valid); 
console.log(email); 

isDefined関数は変数が定義されていないか、それが定義されていない場合、以下のエラーが発生し、代わりにfalseと評価「メール」であるかどうかをチェックするためのものです。

TypeError: $scope.regform.email is undefined 
Stack trace: 
studyseshController/[email protected]://localhost:8000/bundles/framework/js/app.js:17:13 
anonymous/[email protected]://localhost:8000/bundles/framework/js/angular.min.js line 239 > Function:2:147 
bd[b]</<.compile/</</[email protected]://localhost:8000/bundles/framework/js/angular.min.js:284:195 
Mf/this.$get</[email protected]://localhost:8000/bundles/framework/js/angular.min.js:148:351 
Mf/this.$get</[email protected]://localhost:8000/bundles/framework/js/angular.min.js:149:55 
bd[b]</<.compile/</<@http://localhost:8000/bundles/framework/js/angular.min.js:284:245 
[email protected]://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:3:10264 
r.event.add/[email protected]://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:3:8326 
angular.min.js:123:218 
+1

おそらくあなたの答えはこちら:http://stackoverflow.com/questions/27818331/what-is-the-benefit-of-angular-isdefined –

答えて

0

の$ scope.regform.emailが定義されていない場合、私はあなたにも、電子メール内のインデックスが定義されているかどうかを確認することはできません、問題を発見しました。 $ scope.regform.email.validが定義されているかどうかをチェックする前に、$ scope.regform.emailが定義されているかどうかをまずチェックするようにisDefinedを変更しました。これは私が探していた結果をもたらしました。

関連する問題