2016-08-01 10 views
1

I私AngularJS-アプリケーションで次のHTMLコードを持っている:AngularJS指令

<a href="/api/schedulerecordexcel/{{vm.reportInstitution.id}}/{{vm.reportUser.id}}/{{vm.reportYear}}/{{vm.reportMonth}}"><img src="./Excel.PNG" alt="excel" /></a> 

をし、これが正常に動作します。このコードは非常に頻繁に必要なので、私はディレクティブを作ることに決めましたが、ディレクティブを作成する経験はほとんどありません。 これが私の最初のアプローチです:

(function() { 
'use strict'; 

angular 
    .module('myApplication.common') 
    .directive('asExcelDownload', asExcelDownload); 

function asExcelDownload() { 
    var directive = { 
     restrict: 'E', 
     link: link 
    }; 

    return directive; 

    //////////// 

    function link(scope, element, attrs) { 
     // TODO 
    } 
} 
})(); 

は私が右をしています(この機能がOKであれば)リンク機能で何をする場合は、誰かに私に任意のヒントを与えることはできますか?

+0

があなたのExcelをダウンロードするためのHTTP呼び出しを行うサービスを書きます。あなたの指示の中のコントローラからそれを呼び出してください。 – Naveen

答えて

0

あなたはこれを試すことができます。

angular.module('myApplication.common').directive('asExcelDownload', function() { 
    return { 
     restrict: 'E', 
     scope: { 
      vm: '=' 
     }, 
     template: '<a ng-href="/api/schedulerecordexcel/{{vm.reportInstitution.id}}/{{vm.reportUser.id}}/{{vm.reportYear}}/{{vm.reportMonth}}"><img src="./Excel.PNG" alt="excel" /></a>' 
     }; 
    }); 

使用法:

<asExcelDownload vm="vm"></asExcelDownload> 
+0

は完璧に動作します、ありがとう – quma