角度计数不更新

时间:2013-06-09 16:47:37

标签: user-interface angularjs counter

我有一个Angular控制器方法,用于计算等待就座的人数(例如在餐厅)。模型包含设置为true或false的属性。该方法应迭代模型,检查“就座”属性并计算所有真实的属性。

请参阅:http://jsfiddle.net/JoeDredd/nB4tP/

上的简单示例
function seatingCtr($scope) {
$scope.bookings = [{
    name: 'fred',
    seated: true
}, {
    name: 'george',
    seated: false
}, {
    name: 'barney',
    seated: false
}];

$scope.toSeat = function () {
    var count = 0;
    angular.forEach($scope.bookings, function (booking) {
        count += booking.seated ? 0 : 1;
    });
    return count;
};

}

该方法适用于页面加载,并且更改就座下拉菜单正确反映了属性(就座人数),但是当您再次将下拉菜单更改为时,更新的计数器不会反映正确的数字。我无法弄清楚为什么计数器没有正确改变,因为逻辑看起来都很合理。

有人能指出我正确的方向吗?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

问题在于,当您将预订从“已安装”更改为“未就座”时,booking.seated模型的值已更改为字符串“false”而非布尔值false。任何非空的字符串都会计算为true,这就是toSeat()中发生的事情。

Boolean("false");  // == true

Here is your fiddle将true和false替换为字符串“true”和“false”,条件现在正在检查是否booking.seated ===“true”

相关问题