角JS:的JavaScript里面没有自定义指令模板工作 [英] Angular JS: Javascript not working inside custom directive template

查看:171
本文介绍了角JS:的JavaScript里面没有自定义指令模板工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建与下面code角的js自定义指令。

I'm trying to create a custom directive in angular js with the below code.

<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文件如下图所示。

my template.html file is as shown below

<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。
我怎样才能纠正这种?

Only the textbox and button is being rendered. On inspecting element i can see that no contents are getting appended inside the delightmeter division. That is the javascript inside the template file is not getting executed. How can i rectify this?

推荐答案

你应该做的是服用点这个样子,负载与模板控制器:

What you should do is somehting like this, load in the controller with the template:

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'First ';
});

app.directive('exampleDirective', function() {
  return {
    restrict: 'E',
    template: '<p>Hello {{name}}!</p>',
    scope: true,
    controller: "MainCtrl"
    }
})

这篇关于角JS:的JavaScript里面没有自定义指令模板工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆