Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you rotate image in angularjs

Hi I have an image that I want to rotate. There are two buttons left and right which rotate the image 45 degrees in opposite directions. I tried creating a directive using jquery rotate library but it doesn't work. Help?

directive.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>

controller.js

   $scope.rotate= function(angle) {

   $scope.angle=angle;
    };
like image 829
user1424508 Avatar asked Sep 17 '13 02:09

user1424508


1 Answers

You can do the rotation by setting the CSS in the directive

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

Hope it can shed some light on.

Demo

like image 117
zs2020 Avatar answered Sep 27 '22 22:09

zs2020