我有一个具有三个属性的对象。
$scope.badges = {'Full Service': false, 'Negotiable': true, 'Under Warranty': false};
我如何使用extract OR find property
旗帜true
来发送它?
基本上,我想从这个具有true标志的对象中找到属性,然后将其分配给另一个范围变量。
我的HTML
<div ng-repeat="(badge,enabled) in badges" class="checkbox">
<label>
<input type="checkbox" ng-model="badges[badge]" /> {{badge}}
</label>
</div>
角度控制器
$scope.badges = {
'Full Service History': false,
'Negotiable': false,
'Under Warranty': false,
' Urgent': false
};
我想查看用户选择了哪个复选框。
答案 0 :(得分:1)
您可以在控制器中使用此代码完成您想要的任务:
$scope.trueFlags = [];
for(var prop in $scope.badges) {
if($scope.badges.hasOwnProperty(prop) && prop === true) {
$scope.trueFlags.push(prop);
}
}
答案 1 :(得分:1)
您需要迭代每个属性并检查其值是否为真:
$scope.badges = {'Full Sevrice': false, 'Negotiable': true, 'Under Warranty': false};
var keys = [];
for(var name in $scope.badges)
if ($scope.badges[name]) {
keys.push(name);
}
}
答案 2 :(得分:1)
更清洁的解决方案:
$scope.activeKeys = [];
for (var key in $scope.badges) {
if ($scope.badges[key]) {
$scope.activeKeys.push(key);
}
}