简体   繁体   English

条件的HTML href属性

[英]Html href attribute on the condition

I am new to web-development . 我是web-development新手。 I am using angular-js .Here , I have a functionality like , when I click on a button then I get a response from back-end , If that is true then I am changing that button to download button. 我正在使用angular-js这里,我有一个类似的功能,当我单击一个按钮时,我会收到来自后端的响应,如果是真的,那么我会将那个按钮更改为下载按钮。 So, after clicking on this, I am doing an ajax call , with it I get a url , with which I can download a respective file. 因此,单击此按钮后,我正在执行ajax调用,并得到一个url ,可以使用该url下载相应的文件。 So, Here I am using a href to download the file.So, My concern is when I click on download that time only it should get that url and also by using href should download a file. 所以,这里我使用href来下载文件。所以,我关心的是当我单击下载时,只有它应该获取该URL,并且也可以通过使用href下载文件。

My code is like - 我的代码就像-

HTML - HTML-

<button class="btn btn-labeled  btn-info" title= "{{ isAvaliable ? 'Download' : 'click to track'}}" ng-disabled="!file.processed" data-ng-click="performAction(file.attributes.name,file.attributes.cn, isAvaliable)">
         <a ng-href="{{url}}" target="_blank" ></a>
        <i ng-class="isAvaliable ? 'fa fa-download' : 'glyphicon glyphicon-text-width'" aria-hidden="true"></i>
</button>

controller 调节器

$scope.performAction  = function(fileName, cn, isAvaliable) {
                        if(isAvaliable) {
                             $scope.downloadfile(fileName);
                        } else {
                            $scope.moveTofolder(fileName,cn);
                        }
  };

$scope.downloadfile = function(fileName) {
                    uploadService.downloadtracker(fileName)
                        .then(function (response) {
                            $scope.url = response.data;

                        },
                        function (error) {

                        })
                        .finally(function () {
                                                       }
                        );

           };

$scope.moveTofolder = function(fileName,cn) {
  // Here also I am calling some service and  I am getting response .\
  /// Here I am changing the button 
  $scope.isAvaliable = true;        
}

So, Here I am getting some problems, because of the href and the downloadButton . 因此,由于href和downloadButton,在这里我遇到了一些问题。 So, How can I call a function on download click and get the URl and at the same time get that url and use it in the href so that It can download the file as well. 因此,如何在下载单击时调用函数并获取URl,同时获取该URL并在href中使用它,以便它也可以下载文件。 Href and downloadButton are for the same purpose.Thanks in advance. HrefdownloadButton的目的是相同的。

You just need to add download attribute in your button 您只需要在按钮中添加下载属性

<button class="btn btn-labeled  btn-info" title= "{{ isAvaliable ? 'Download' : 'click to track'}}" ng-disabled="!file.processed" data-ng-click="performAction(file.attributes.name,file.attributes.cn, isAvaliable)" ng-click=“downloadfile
({{url}})”>
         <!— <a ng-href="{{url}}" target="_blank" ></a> —>
        <i ng-class="isAvaliable ? 'fa fa-download' : 'glyphicon glyphicon-text-width'" aria-hidden="true"></i>
</button>

Add $http in controller 在控制器中添加$ http

$scope.downloadfile = function(fileName) {
                //here I am calling a service and I am getting a responce which contains a url.
            $scope.$emit('download-start');

            $http.get(fileName).then(function(response) {
                $scope.$emit('downloaded', response.data);
            });

                    $scope.url =  responce.data;
               }

Hope this help you 希望这对您有帮助

Try like below. 尝试如下。 Don't mingle <a> and <button> because you have button click and also anchor click event. 不要混入<a> and <button>因为您有按钮单击并且还锚定了单击事件。

<a ng-href="{{url}}" ng-click="Download()" ></a>

$scope.Download = function(){
//some other code
window.open($scope.url);
}



 <button class="btn btn-labeled  btn-info" title= "{{ isAvaliable ? 'Download' : 'click to track'}}" ng-disabled="!file.processed" data-ng-click="performAction(file.attributes.name,file.attributes.cn, isAvaliable);downloadfile
(url)">

        <i ng-class="isAvaliable ? 'fa fa-download' : 'glyphicon glyphicon-text-width'" aria-hidden="true"></i>
</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM