javascript - AngularJS - 引用错误 : "someScopeVar" is not defined

标签 javascript angularjs

我正在使用 AngularJS 构建一个反馈表单,其中用户数据存储在 $scope.feedback 中。现在我想包含一个进度条并且需要确定百分比。

    // $scope-variable to hold the data.
    $scope.feedback = {
        title : undefined,
        name : undefined,
        email : undefined,
        tel : undefined
    };

    // Get the total amount of attributes of $scope.feedback
    var totalAmountOfAttributesOfFeedback = 0;
    for (var k in $scope.feedback) {
        if ($scope.feedback.hasOwnProperty(k)) {
            ++totalAmountOfAttributesOfFeedback;
        }
    }

    // Get the amount of defined attributes of $scope.feedback
    // to determine the percentage of Progress
    $scope.determinePercentageOfAttributesDefined = function () {
        var amountOfDefinedAttributesOfFeedback = 0;
        for (var l in $scope.feedback) {
            if (angular.isDefined($scope.feedback[l])) {
                ++amountOfDefinedAttributesOfFeedback;
            }
        }
        $scope.percentageOfAttributesDefined = (amountOfDefinedAttributesOfFeedback / totalAmountOfAttributesOfFeedback)*100;
        console.log(percentageOfAttributesDefined);
    };

为了计算百分比,我计算了 $scope.feedback 的属性总数。 要计算用户已填写的属性数量,可以使用 determinePercentageOfAttributesDefined 函数,只要触摸输入字段,就会调用该函数。

问题是,变量 $scope.percentageOfAttributesDefined 没有定义,我在浏览器控制台中收到以下错误:

ReferenceError: percentageOfAttributesDefined is not defined

我也尝试在我的 Controller 中的其他地方创建变量,但得到了同样的错误。有人知道原因吗? 可以毫无问题地访问其他范围变量,例如 $scope.feedback

感谢您抽出时间:)

最佳答案

console.log($scope.percentageOfAttributesDefined);

这应该可以解决您的问题。

关于javascript - AngularJS - 引用错误 : "someScopeVar" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39242134/

相关文章:

javascript - 嵌套子状态中的 Angular ui 路由和多个 View

javascript - RemoveEventListener 在 Firefox 版本 58 中不起作用

angularjs - 如何在 ng-repeat 中显示元素一次

AngularJS $resource 不发送自定义 header

javascript - 如何在 `popupopen` 事件期间识别 Leaflet 的标记?

javascript - CKEditor:在粘贴上应用 removeFormat

javascript - 如何从 JavaScript 调用 CSS

带有多个标记的 AngularJS 谷歌地图

AngularJS : Render HTML in $scope variable in view

javascript - : show or hide element after button click 的 Angular 方式