2015-04-25 76 views
0

我已經創建了一個使用Angular JS的自定義指令。我在控制器裏面有一個函數,我試圖從文本框中調用ng-change事件。但函數沒有被調用。這裏是我的代碼:ng變化事件不起作用

<script> 
    var delightMeterApp = angular.module('delightMeterApp', []); 
    delightMeterApp.directive('delightMeter', function() { 
     return { 
      scope: true, 
      restrict: 'E', 
      template: '<div id="delightmeter"></div>', 
      link: function (scope, element) { 


       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); 

       document.getElementById("arc1").setAttribute("d", describeArc(200, 200, 100, -90, -56)); 
       document.getElementById("arc2").setAttribute("d", describeArc(200, 200, 100, -54, -20)); 
       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; 
       } 
      }, 
      template: '<div id="delightmeter"></div>', 
      controller: "delightMeterController" 

     }; 
    }); 
    delightMeterApp.controller('delightMeterController', function ($scope) { 

     $scope.fun = function() { 
      alert("called"); 
     } 
    }); 
</script> 

以下是我的HTML

<div ng-app="delightMeterApp" ng-controller="delightMeterController"> 
    <delight-meter ng-model="delightScore"></delight-meter> 
    <input id="Text1" type="text" ng-change="fun()" /> 
</div> 

是否正確使用同樣的控制器無論對於自定義指令和輸入控制。我在這裏做錯了什麼?

+0

你爲什麼要使用CONTRO無論如何,你的指令中都有勒號? –

+0

任何控制檯錯誤? –

+0

我是角JS的新手。我得到了在這個問題中的另一個問題的答案 - http://stackoverflow.com/questions/29865129/angular-js-javascript-not-working-inside-custom-directive-template - 答案說使用控制器的自定義指令在其中包含javascript –

回答

1

Sooraj,

像什麼NexusDuck說,你不應該操縱你的控制器中的DOM。我已經重組了你的代碼,讓Directive和Controller中的DOM操作只需調用Rotate Needle函數。 Rotate Needle函數在指令中被調用。

這個HTML

<div ng-controller="delightMeterController"> 
<delightmeter delight-meter-reference='delightMeterReference'></delightmeter> 
<input id="txtScore" type="text" ng-model="delightScore" ng-change="delightMeterReference.RotateNeedle(delightScore)" />{{delightScore}} 
</div> 

這是你的指令:

.directive('delightmeter', function() { 
    function link($scope, $element, $attrs) { 

     var meter = $element[0]; 
     console.log(meter); 

     document.getElementById("arc1").setAttribute("d", describeArc(200, 200, 100, -90, -56)); 
     document.getElementById("arc2").setAttribute("d", describeArc(200, 200, 100, -54, -20)); 
     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; 
     } 

     function RotateNeedle(delightScore) { 
      console.log(delightScore); 
      $('.needleset').css({ 
       "transform": "rotate(" + delightScore + "deg)", 
       "transform-origin": "50% 95%" 
      }); 
     } 

     if ($scope.delightMeterReference) { 
      $scope.delightMeterReference.RotateNeedle = function (delightScore) { 
       RotateNeedle(delightScore); 
      } 
     } 
    } 
    return { 
     restrict: 'E', 
     templateUrl: 'components/comp01/comp01.html', 
     scope: { 
      delightMeterReference: '=' 
     }, 
     link: link 
    }; 
}) 

這是你的控制器

.controller('delightMeterController', function ($scope) { 

    $scope.delightScore = 0; 
    $scope.delightMeterReference = {}; 


}) 

感謝, Shivas