【问题标题】:angularJS Error: variable is undefined, even when inside isDefined() functionangularJS 错误:变量未定义,即使在 isDefined() 函数中也是如此
【发布时间】:2017-05-14 14:49:17
【问题描述】:

我检查了类似的问题,但没有一个适用于这种情况。

我有一个 Angular 应用程序,我正在尝试手动验证它,但我遇到了问题。

为简单起见,我只包括电子邮件字段,表单字段已在另一个函数中验证,并且该函数将 $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 函数用于检查变量是否已定义,但如果未定义,则会发生以下错误,而不是 'email' 评估为 false。

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

【问题讨论】:

标签: javascript jquery angularjs


【解决方案1】:

我发现了这个问题,如果 $scope.regform.email 没有定义,你甚至无法检查 email 中的索引是否已经定义。我将 isDefined 更改为首先检查 $scope.regform.email 是否已定义,然后再检查 $scope.regform.email.valid 是否已定义。这产生了我正在寻找的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2016-02-17
    相关资源
    最近更新 更多