AngularJS ng-switch,我做错了什么?

时间:2015-12-17 19:25:52

标签: javascript angularjs

我对以下代码中的ng-switch情况感到非常沮丧:

<div class="UPLcontent" ng-model="uploadFiles">
    <ul class="UPLfiles" ng-repeat="file in uploadFiles track by $index">
        <li class="UPLicon"><i class="{{file.type | typeIcon }}"></i></li>
        <li class="UPLinfo"><span>{{file.name | html}}</span><span>{{file.size | fileSize}}</span></li>
        <li class="UPLtool" ng-switch on="toolActionStage" >
            <i class="fi-x-circle" ng-click="removeFromUploadList($index)"  ng-switch-default="delete"></i>
            <div ng-switch-when="progress">

                <div ang:round:progress data-round-progress-model="roundProgressData"
                    data-round-progress-width="30"
                    data-round-progress-height="30"
                    data-round-progress-outer-circle-width="3"
                    data-round-progress-inner-circle-width="0"
                    data-round-progress-outer-circle-radius="10"
                    data-round-progress-inner-circle-radius="0"
                    data-round-progress-label-font="0pt Arial"
                    data-round-progress-outer-circle-background-color="red"
                    data-round-progress-outer-circle-foreground-color="#67CB8C"
                    data-round-progress-inner-circle-color="#505769"
                    data-round-progress-label-color="#67CB8C"
                    ></div>
            </div>

        </li>
    </ul>   
</div>

我的ng-switch-default显示得很好但是在显示&#34;进度&#34;我得到一个空白的容器...我已经尝试了每一个方向,我不能让它正常工作。有什么指针吗?

更新:

HEre是圆形渐进式指令,什么搞乱???

angular.module('angular.directives-round-progress', []).directive('angRoundProgress', [function () {
  var compilationFunction = function (templateElement, templateAttributes, transclude) {
    if (templateElement.length === 1) {
      var node = templateElement[0];

      var width = node.getAttribute('data-round-progress-width') || '400';
      var height = node.getAttribute('data-round-progress-height') || '400';

      var canvas = document.createElement('canvas');
      canvas.setAttribute('width', width);
      canvas.setAttribute('height', height);
      canvas.setAttribute('data-round-progress-model', node.getAttribute('data-round-progress-model'));

      node.parentNode.replaceChild(canvas, node);

      var outerCircleWidth = node.getAttribute('data-round-progress-outer-circle-width') || '20';
      var innerCircleWidth = node.getAttribute('data-round-progress-inner-circle-width') || '5';

      var outerCircleBackgroundColor = node.getAttribute('data-round-progress-outer-circle-background-color') || '#505769';
      var outerCircleForegroundColor = node.getAttribute('data-round-progress-outer-circle-foreground-color') || '#12eeb9';
      var innerCircleColor = node.getAttribute('data-round-progress-inner-circle-color') || '#505769';
      var labelColor = node.getAttribute('data-round-progress-label-color') || '#12eeb9';

      var outerCircleRadius = node.getAttribute('data-round-progress-outer-circle-radius') || '100';
      var innerCircleRadius = node.getAttribute('data-round-progress-inner-circle-radius') || '70';

      var labelFont = node.getAttribute('data-round-progress-label-font') || '50pt Calibri';

      return {
        pre: function preLink(scope, instanceElement, instanceAttributes, controller) {
          var expression = canvas.getAttribute('data-round-progress-model');
          scope.$watch(expression, function (newValue, oldValue) {
            // Create the content of the canvas
            var ctx = canvas.getContext('2d');
            ctx.clearRect(0, 0, width, height);

            // The "background" circle
            var x = width / 2;
            var y = height / 2;
            ctx.beginPath();
            ctx.arc(x, y, parseInt(outerCircleRadius), 0, Math.PI * 2, false);
            ctx.lineWidth = parseInt(outerCircleWidth);
            ctx.strokeStyle = outerCircleBackgroundColor;
            ctx.stroke();

            // The inner circle
            ctx.beginPath();
            ctx.arc(x, y, parseInt(innerCircleRadius), 0, Math.PI * 2, false);
            ctx.lineWidth = parseInt(innerCircleWidth);
            ctx.strokeStyle = innerCircleColor;
            ctx.stroke();

            // The inner number
            ctx.font = labelFont;
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillStyle = labelColor;
            ctx.fillText(newValue.label + '%', x, y);

            // The "foreground" circle
            var startAngle = - (Math.PI / 2);
            var endAngle = ((Math.PI * 2 ) * newValue.percentage) - (Math.PI / 2);
            var anticlockwise = false;
            ctx.beginPath();
            ctx.arc(x, y, parseInt(outerCircleRadius), startAngle, endAngle, anticlockwise);
            ctx.lineWidth = parseInt(outerCircleWidth);
            ctx.strokeStyle = outerCircleForegroundColor;
            ctx.stroke();
          }, true);
        },
        post: function postLink(scope, instanceElement, instanceAttributes, controller) {}
      };
    }
  };

  var roundProgress = {
    compile: compilationFunction,
    replace: true
  };
  return roundProgress;
}]);

2 个答案:

答案 0 :(得分:0)

您应该将ng-switch on="toolActionStage"替换为ng-switch="toolActionStage"。据我所知,你现在的语法无效。

答案 1 :(得分:0)

ng-switch创建一个新范围,指令未正确处理它。

您可以通过向帖子功能添加debugger并查看scope的内容来轻松查看此内容。

如果您在没有ng-switch的情况下运行指令,则roundProgressData符合预期scope

在您的情况下,roundProgressData可以找到scope.$parent

更新2:

我同意tasseKATT:只需使用ng-show。这是一个工作小提琴:https://jsfiddle.net/masa671/m63ej0hf/