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;
};
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With