我想用下面的代碼在角JS中創建自定義指令。角JS:Javascript不工作在自定義指令模板內
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var app = angular.module('columnarAdditionApp', []);
app.directive('addition', function() {
return {
restrict: 'E',
templateUrl: 'template.html'
};
});
</script>
<body>
<div ng-app="columnarAdditionApp" >
<addition></addition>
</div>
</body>
我template.html文件如下圖所示
<div id="delightmeter"></div>
<input id="value" type="text" />
<button id="test">Click Here</button>
<script>
var newdiv = jQuery('<div/>', {
id: 'delightContainer',
class: 'singlenote'
}).appendTo('#delightmeter');
var appendstring = "";
appendstring += "<svg width='500px' height='300px' version='1.1' xmlns='http://www.w3.org/2000/svg>'";
appendstring += "<g>";
appendstring += "<text x='100' y='220' fill='black'>0</text>";
appendstring += "<text x='300' y='220' fill='black'>100</text>";
appendstring += "<path class='arc' id='arc1' d='' />";
appendstring += "<path class='arc' id='arc2' d='' />";
appendstring += "<path class='arc' id='arc3' d='' />";
appendstring += "<path class='arc' id='arc4' d='' />";
appendstring += "<path class='arc' id='arc5' d='' />";
appendstring += "<g class='needleset'>";
appendstring += "<circle class='needle-center' cx='200' cy='200' r='5'></circle>";
appendstring += "<path class='needle' d='M 195 198 L 200 100 L 205 202'></path>";
appendstring += "</g></g></svg>";
newdiv.append(appendstring);
$("#test").click(function() {
var rotateval = $("#value").val();
$('.needleset').css({
"transform": "rotate(" + rotateval + "deg)",
"transform-origin": "50% 95%"
});
}
);
document.getElementById("arc1").setAttribute("d", describeArc(200, 200, 100, -90, -56));
document.getElementById("arc2").setAttribute("d", describeArc(200, 200, 100, -54, -20));`enter code here`
document.getElementById("arc3").setAttribute("d", describeArc(200, 200, 100, -18, 16));
document.getElementById("arc4").setAttribute("d", describeArc(200, 200, 100, 18, 52));
document.getElementById("arc5").setAttribute("d", describeArc(200, 200, 100, 54, 90));
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees - 90) * Math.PI/180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeArc(x, y, radius, startAngle, endAngle) {
var start = polarToCartesian(x, y, radius, endAngle);
var end = polarToCartesian(x, y, radius, startAngle);
var arcSweep = endAngle - startAngle <= 180 ? "0" : "1";
var d = [
"M", start.x, start.y,
"A", radius, radius, 0, arcSweep, 0, end.x, end.y
].join(" ");
return d;
}
</script>
只有文本框和按鈕被渲染。 在檢查元素我可以看到沒有內容被追加delightmeter
司內。這是模板文件中的JavaScript沒有執行。 我該如何糾正這一點?
請勿將腳本放在模板中!這也是**不是**的角度! – Michelangelo