我正在使用 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/