【发布时间】:2016-07-18 09:19:21
【问题描述】:
我正在尝试使用 html 单选按钮显示(文件名项目的)动态列表。请在下面找到相同的代码:
<html>
<head>
<script src="angular-v1.5.5.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<label>
<input type="radio" ng-model="inputCreatedBy" value="byX" ng-value="true"
ng-click="toggleSelection('byX')"> by X
<input type="radio" ng-model="inputCreatedBy" value="byAll" ng-value="false"
ng-click="toggleSelection('byAll')"> by All
</label>
<br/><br/>
<label ng-repeat="file in displayFiles">
{{ file.name }}
</label>
</div>
</body>
<script>
var app = angular.module("myApp",[]);
app.controller('myCtrl', function ($scope, filterFilter) {
$scope.files = [
{ name: 'file1', createdBy: 'X' },
{ name: 'file2', createdBy: 'X' },
{ name: 'file3', createdBy: 'Y' },
{ name: 'file4', createdBy: 'Y' }
];
$scope.displayFiles = [];
$scope.toggleSelection = function(selectionType) {
if(selectionType == 'byX') {
for(i=0;i<$scope.files.length;i++) {
if($scope.files[i].createdBy =='X') {
$scope.displayFiles.push($scope.files[i]);
}
}
} else if(selectionType == 'byAll') {
$scope.displayFiles = $scope.files;
}
};
});
</script>
</html>
我在使用此代码时遇到以下问题:
(1) 默认情况下不选择单选按钮选项“by X”,即使我将其标记为 ng-value 'true'。
(2) 选择“by All”选项后,我无法选择“by X”选项。
您能帮忙解决这些问题吗?
【问题讨论】:
标签: javascript html angularjs