源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <!-- directive: runoob-directive --> <script> var app = angular.module("myApp", []); app.directive("runoobDirective", function() { return { restrict : "M", replace : true, template : "<h1>自定義指令!</h1>" }; }); </script> <p><strong>注意:</strong> 我們需要在該實例添加 <strong>replace</strong> 屬性, 否則評論是不可見的。</p> <p><strong>注意:</strong> 你必須設置 <b>restrict</b> 的值為 "M" 才能通過注釋來調用指令。</p> </body> </html>
運行結果