我在TypeError: Cannot read property 'allowed' of undefined
的最后一个元素上遇到了一个奇怪的问题$scope.leaveType
。
更新:
感谢@dusky修复了这个问题,但现在我在控制台中遇到错误,但应用正常这里是截图
控制器的完整代码是here
似乎没有得到$scope.myLeaves[id]
$scope.myLeaves = { "1": 18, "2": 12, "3": 0, "4": 0, "5": 1, "6": 1, "7": 1};
请帮助我在这里错过的。
$scope.leaveType = [
{ "id": "1", "name": "Paid Leave", "allowed": 18},
{ "id": "2", "name": "Caual Leave", "allowed": 12},
{ "id": "3", "name": "Maternity Leave", "allowed": 0},
{ "id": "4", "name": "Paternity Leave", "allowed": 0},
{ "id": "5", "name": "Celebration Leave", "allowed": 1},
{ "id": "6", "name": "Compensatory Off","allowed": 1},
{ "id": "7", "name": "Optional Holiday Leave", "allowed": 1}
];
$scope.myLeaves = { "1": 18, "2": 12, "3": 0, "4": 0, "5": 1, "6": 1, "7": 1};
$scope.taken = function (index) {
var allowed = $scope.leaveType[index].allowed,
leaveTypeId = $scope.leaveType[index].id,
taken = (parseFloat(allowed,10)) - (parseFloat($scope.myLeaves[leaveTypeId], 10));
return taken;
}
//Get remaining leaves in a category
$scope.remaining = function (index) {
var allowed = $scope.leaveType[index].allowed,
leaveTypeId = $scope.leaveType[index].id,
remain = (parseFloat(allowed,10)) - $scope.taken(leaveTypeId, allowed);
return remain;
}
这是Plunker
答案 0 :(得分:1)
你剩下的功能是错误的。 $scope.taken
将索引作为参数。顺便说一下:parseFloat不带2个参数(parseInt)。这里不需要它,因为允许已经是一个数字。
错误在这一行:
remain = (parseFloat(allowed,10)) - $scope.taken(leaveTypeId, allowed);
应该是:
remain = allowed - $scope.taken(index);
所以它变成了:
$scope.remaining = function (index) {
var allowed = $scope.leaveType[index].allowed,
remain = allowed - $scope.taken(index);
return remain;
}