Javascript 如何在angularjs中旋转图像

Javascript 如何在angularjs中旋转图像,javascript,jquery,angularjs,rotation,angularjs-directive,Javascript,Jquery,Angularjs,Rotation,Angularjs Directive,嗨,我有一个我想旋转的图像。左右两个按钮可将图像向相反方向旋转45度。我尝试使用jquery rotate库创建一个指令,但它不起作用。帮忙 指令.js .directive('rotate', function() { return { restrict:'A', link: function(scope, element, attrs) { console.log('hi'); // watch the degrees attribute, and

嗨,我有一个我想旋转的图像。左右两个按钮可将图像向相反方向旋转45度。我尝试使用jquery rotate库创建一个指令,但它不起作用。帮忙

指令.js

.directive('rotate', function() {
return {
    restrict:'A',
    link: function(scope, element, attrs) {
      console.log('hi');
        // watch the degrees attribute, and update the UI when it changes
        scope.$watch(attrs.degrees, function(rotateDegrees) {
            console.log(rotateDegrees);
            //transform the css to rotate based on the new rotateDegrees
  element.rotate(rotateDegrees);    

        });
    }
}
}))

template.html

<p><img degrees='angle' rotate id='image' src='myimage.jpg'/> </p>
<a ng-click="rotate('-45')">Rotate Left</a>
<a ng-click="rotate('45')">Rotate Right</a>

您可以通过在指令中设置CSS来进行旋转

app.directive('rotate', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(attrs.degrees, function (rotateDegrees) {
                console.log(rotateDegrees);
                var r = 'rotate(' + rotateDegrees + 'deg)';
                element.css({
                    '-moz-transform': r,
                    '-webkit-transform': r,
                    '-o-transform': r,
                    '-ms-transform': r
                });
            });
        }
    }
});
希望它能给我们一些启示


我只是想告诉你我的旋转解决方案

<img src="{{Image_URL}}" id="img{{$index}}" class="CssImage" />
<a href=""><span class="glyphicon glyphicon-repeat" id="rotateIcon" data-ng-init="rd=1;" data-ng-click="RotateImage('img'+ $index,rd);rd=rd+1;rd==4?rd=0:''"></span></a>

作为一种选择,任何评论都将受到欢迎。

是的,但是有任何方法可以获得旋转后的图像url吗?只需调用按钮上的函数(ng click=“RotateImageRight()”),还有另一种方法。您可以使用ng样式:
$scope.RotateImageRight = function () {
            z = z + 1;
            var img;
            if (z === 1) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-90deg)";
                img.style.width = "725px";

            }

            else if (z === 2) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-180deg)";
                img.style.width = "870px";
            }

            else if (z === 3) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-270deg)";
                img.style.width = "725px";
            }

            else if (z === 4) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-360deg)";
                img.style.width = "870px";
            } else {
                z = 0;
            }

        }
$scope.RotateImage = function (id,deg) {
    var deg = 90 * deg;
    $('#' + id).css({
        '-webkit-transform': 'rotate(' + deg + 'deg)',  //Safari 3.1+, Chrome  
        '-moz-transform': 'rotate(' + deg + 'deg)',     //Firefox 3.5-15  
        '-ms-transform': 'rotate(' + deg + 'deg)',      //IE9+  
        '-o-transform': 'rotate(' + deg + 'deg)',       //Opera 10.5-12.00  
        'transform': 'rotate(' + deg + 'deg)'          //Firefox 16+, Opera 12.50+  

    });
}
$scope.RotateImageRight = function () {
            z = z + 1;
            var img;
            if (z === 1) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-90deg)";
                img.style.width = "725px";

            }

            else if (z === 2) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-180deg)";
                img.style.width = "870px";
            }

            else if (z === 3) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-270deg)";
                img.style.width = "725px";
            }

            else if (z === 4) {
                img = document.getElementById("rotate90");
                img.style.transform = "rotate(-360deg)";
                img.style.width = "870px";
            } else {
                z = 0;
            }

        }