2017-10-19 115 views
0

當我們有以下指令動態不顯示:內容指令加載使用templateUrl

angular.module("MyApp") 
    .directive('documentViewer', function() { 
     return { 
      restrict: 'E', 
      link: function (scope, element, attrs) { 
      }, 
      templateUrl: "templates/documentViewer.tpl.html" 
     } 
    }); 

在頁面上,我們有一個網格,在網格中的每一列有用戶可以點擊一個鏈接。當用戶點擊該鏈接,我們試圖從頁面的控制器內顯示彈出:

self.viewDocument = function (docId) { 
    var title = "Document Viewer"; 
    var body = $compile('<document-viewer></document-viewer>')($scope); 
    showBootstrapModalDialog(title, body, true, true, false); 
}; 

在對Chrome瀏覽器開發工具的網絡選項卡上,我可以看到指定的模板被獲取,但是,內容不顯示在彈出窗口中。你可以在這裏看到:Screenshot of pop-up

這裏是模板的內容:

<div> 
    document viewer template 
</div> 

我缺少什麼?

回答

0

爲什麼你需要使用$ compile?

這樣的事情肯定做的伎倆

self.viewDocument = function (docId) { 
$scope.currentDoc = docId; 
}; 

<div> 
    ... 
    <document-viewer ng-if='currentDoc'></document-viewer> 
    ... 
</div> 
+0

感謝您的建議。只要點擊一個鏈接,文檔查看器的內容就會不同,這種方法只調用鏈接()函數一次,內容不變,有什麼想法? –

+0

將您需要的所有內容作爲屬性傳遞給指令。那些將可用的鏈接功能,你可以做任何你想要的東西。 <文檔查看器attR1位= '富' attR2位= '酒吧' NG-如果= 'currentDoc'>

+0

我使documentId入指令,然而,鏈路函數只得到當第一個鏈接被點擊時稱爲1次。點擊另一個鏈接後,不會再次調用,該指令的內容與第一個鏈接點擊的內容保持一致。 –