2017-06-15 60 views
1

我只是玩角度指令,但不知何故評論添加指令的方式沒有顯示出來。這是標記。AngularJS指令作爲評論

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <script src="angular.js"></script> 
</head> 

    <body ng-app="directive-app"> 

     <first-directive></first-directive> 

     <div first-directive></div> 

     <div class=first-directive></div> 

     <!-- directive: first-directive -->  

     <script src="main.js"></script> 


</body> 
</html> 

指令定義

var app=angular.module('directive-app',[]); 

app.directive("firstDirective", function() { 
    return { 
     restrict : "EACM", 
     templateUrl : 'template.html' 
    }; 
}); 

在template.html有一個h1元素。但是添加指令的註釋方式並未在用戶界面中顯示出來,甚至在這種情況下還需要註釋方法。

回答

3

設置replace選項true如下

app.directive("firstDirective", function() { 
    return { 
    restrict: "EACM", 
    replace: true, 
    templateUrl: 'template.html' 
    }; 
}); 

這裏的demo

+0

你可以給你一些解釋。爲什麼替換隻需要在註釋 –

+0

@MahtabAlam的情況下才需要首先,熟悉'replace'選項,請參閱https://stackoverflow.com/a/15286383/87972。當'replace'爲'false'時,Angular會嘗試將模板內容放入註釋中,這就是爲什麼它不可見。當'replace'爲'true'時,Angular會將模板內容替換爲註釋,這就是爲什麼它是可見的。 – Hoa

+0

謝謝@Hoa –