我有一份学生名单。
对于一些学生,我必须显示一个链接,点击这个链接,他们的进度卡应该加载(这是一个.pdf文件)。对其他人来说,不应显示这种联系。
我已经这样做了,给相同的学生身份证给他们的进步卡,如果是这个特定的学生,然后显示链接,否则,不要显示链接。我只能为5到6个学生这样做。我不能为1000名学生这样做。
以下是我的AngularJS代码:
if (response.student_id == 'SD0001' || response.student_id == 'SD0002' || response.student_id == 'SD0004') {
$scope.pdfData = true;
$scope.StudentPDFFile = response.student_id+'.pdf';
} else {
$scope.pdfData = false;
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<body id="ng-app" ng-app>
<div ng-switch on="pdfData">
<div ng-switch-when="false"></div>
<div ng-switch-when="true">
<span class="font-10">Download Performance Report</span>
<a href="http://www.example.com/s-pdf/{{StudentPDFFile}}" target="_blank"> click here</a>
</div>
</div>
</body>
这里我还没有指定控制器,这是骨架功能正在为我工作。
1000张唱片我怎么能这么做?
发布于 2014-10-16 17:35:38
您可以发出http请求,以确定该文件是否存在,只要您与pdf文件位于同一域中。
对于给定的学生ID (在代码中它看起来是response.student_id),它如下所示:
if(response.student_id) {
var url = 'http://www.example.com/s-pdf/' + response.student_id + '.pdf';
var request = new XMLHttpRequest();
request.open('HEAD', url, false);
request.send();
if(request.status == 200) {
$scope.pdfData = true;
$scope.StudentPDFFile = response.student_id+'.pdf';
} else {
$scope.pdfData = false;
}
} else {
$scope.pdfData = false;
}'HEAD'是确保文件存在所必需的;您也可以使用'GET',但在这种情况下似乎不需要整个文件。
发布于 2015-03-06 15:12:23
这是另一种方式。您可以使用此指令创建指令并验证attrib,并在元素中设置所需的任何内容。
JS
angular.module('myApp')
.directive('imgCheck', ['$http', function($http){
return {
link: function(scope, element, attrs){
$http.get('/img/'+attrs.imgCheck)
.success(function(data, status){
if(status==200){
attrs.$set('src','/img/'+attrs.imgCheck)
}else{
attrs.$set('src','/img/not-found.png')
}
})
.error(function(data,status){
if(status==200){
attrs.$set('src','/img/'+attrs.imgCheck)
}else{
attrs.$set('src','/img/not-found.png')
}
});
}
};]);
<figure class="podcast-item col-md-2" ng-repeat="image in images">
<a href="#">
<img class="img-thumbnail" img-check="{{image.url}}" />
</a>
</figure>发布于 2014-10-22 11:41:27
ng-show似乎更合适:
<div ng-show="pdfData">
<span class="font-10">Download Performance Report</span>
<a href="http://www.example.com/s-pdf/{{StudentPDFFile}}" target="_blank"> click here</a>
</div>对于1000,将其放在一个ng-repeat中,那么您应该更好地测试一个请求中的所有PDF,并将其保存在一个缓存列表中。
https://stackoverflow.com/questions/26270862
复制相似问题