举个例子:http://jsfiddle.net/KWLu7/16/
我试图计算总体重要性值,即每个选择的重要性之和乘以其父标准重要性:

var watch = $scope.$watch("prioritizationProfile", function() {
    $scope.overallImportance = 0;

    for(var j = 0; j < $scope.prioritizationProfile.length; j++) {
        var criterionGroup = $scope.prioritizationProfile[j].criterionGroup;

        for(var i = 0; i < criterionGroup.length; i++) {

            var criterionChoices = criterionGroup[i].choices;

            for(var k = 0; k < criterionChoices.length; k++) {
                console.log(criterionChoices[k]);
                if(criterionChoices[k].selected) {
                    $scope.overallImportance +=
                        criterionChoices[i].importance *
                        criterionGroup[i].importance;
                }
            }
        }
    }
}, true);

如果你尝试它,你会看到最大值总是22。我在这里做错什么了?

最佳答案

您在prioritizationProfileWatch的内部for循环中使用了错误的索引
改变

$scope.overallImportance += criterionChoices[i].importance * criterionGroup[i].importance;


$scope.overallImportance += criterionChoices[k].importance * criterionGroup[i].importance;

关于javascript - 计算整体重要性。 AngularJS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24257019/

10-10 03:42