angularjsangularjs-scopeangular-ngmodelangularjs-ng-disabled

Download button not responding to ng-disabled in angularjs


I have a webpage which displays contents of a folder/directory upon clicking on it. If the contents of a folder has files, then download button is activated. If the contents of the folder is a directory, download button is inactive.

Here is my code that is supposed to make that work. But the Download button remains active all the time and never goes inactive. Can somebody point out where I'm going wrong ?

$scope.IsFile = function(filenames){
		forEach(filenames, function(filename){
			if(filename.indexOf(".zip") > 1|| filename.indexOf(".txt") >1 || filename.indexOf(".xml") > 1 )	{
				return true;
			}
			else {
				return false;
			}
		});
	};
<div class="col-md-3" id="CTP Jobs">
		<div ng-click="GetListOfFilesonCTP('/home/topas/rda_app/JOBS/')">
			<a href="">
				<h3>
					JOBS <small> <span class="btn-group"
						role="group">
							<button type="button" class="btn btn-default" ng-disabled="IsFile(listOfFilesOnJobs)"
								ng-click="download()">Download</button>
					</span>
					</small>
				</h3>
			</a>
		</div>
		<table class="table table-striped"
			ng-init="GetListOfFilesonCTP('/home/topas/rda_app/JOBS')">
			<tr ng-repeat="jobs in listOfFilesOnJobs"
				ng-click="GetListOfFilesonCTP(getPath('/home/topas/rda_app/JOBS/', jobs))">
				<!--'/home/topas/rda_app/JOBS/'+ jobs + '/'  -->
				<td><a href="">{{jobs}}</a></td>
			</tr>
		</table>
	</div>


Solution

  • Your IsFile function does not return anything explicitly (i.e. it returns undefined) and ng-disabled interprets that as false. The return true and return false on lines 4 and 7 are relative to the function passed to forEach, which is probably not what you want. Did you mean to filter the list of files and check if any files are remaining? Something like this would do:

    $scope.isFile = function(filenames) {
        return filenames.filter(function(filename) {
            return filename.indexOf(".zip") > 1 || 
                filename.indexOf(".txt") > 1 || 
                filename.indexOf(".xml") > 1
        }).length > 0
    }