AngularJS - ng-Style not changing when updated in the directive - css

So, here is a custom directive to resize an element and it's contents based on a screen size change. I can see that the value of 'scalify' is changing how I would like it to, but the style itself is not changing. I know that the styles work when applied with static values directly in the css file.
.directive("card", function(){
return{
restrict : "E",
controller: function($scope, $element, $attrs){
var w = this;
window.onresize = function () {
$scope.scalify = {
'-ms-zoom': window.innerHeight/675,
'-moz-transform': 'scale(' + window.innerHeight/675 + ')',
'-o-transform': 'scale(' + window.innerHeight/675 + ')',
'-webkit-transform': 'scale(' + window.innerHeight/675 + ')'
};
$scope.$apply();
}
}
};
})
And here's how I use the ng-Style in the HTML
<card ng-style="scalify">
...
</card>

Hi please see here: http://run.plnkr.co/jfeF9NcPcGroNCsg/ probably you need to work a bit on your css transformation but style is changing when you change size of window as you wanted
app.directive("card", function($window) {
return {
restrict: "E",
link: function(scope, element, attrs) {
scope.onResizeFunction = function() {
//$scope.windowHeight = $window.innerHeight;
// $scope.windowWidth = $window.innerWidth;
var scalify = {
'-ms-zoom': $window.innerHeight / 675,
'-moz-transform': 'scale(' + $window.innerHeight / 675 + ')',
'-o-transform': 'scale(' + $window.innerHeight / 675 + ')',
'-webkit-transform': 'scale(' + $window.innerHeight / 675 + ')',
'background-color': 'red'
};
element.css(scalify);
console.log(element);
};
angular.element($window).bind('resize', function() {
scope.onResizeFunction();
scope.$apply();
});
}
}
});

Related

unable to apply css styles to an svg image embedded in a div tag.?

Hi I am having an angular 5 project . I am drawing an bar graph using d3js library. In my bar graph there are vertical bars to which I would like to apply styles. However it is not working as the bars are displayed with default black colour as show below.
The following is my component html code, where i have a div tag named "histogramHolder" to which i will append the svg from my component ts file.
performance.component.html
<div class="row">
<div class="col-1"></div>
<div class="col-10" id="histogramHolder">
</div>
<div class="col-2"> </div>
</div>
the following is my component scss file.
performance.component.scss
.bar {
fill: steelblue;
}
the following is the code snippet where i create an svg image and append to the div tag named "histogramHolder"
import * as d3 from "d3";
#Component({
selector: 'pc-performance',
templateUrl: './performance.component.html',
styleUrls: ['./performance.component.scss']
})
export class PerformanceComponent implements OnInit {
ngOnInit() {
const data=[{"key":"2019-09-11","documentCount":149002},{"key":"2019-09-12","documentCount":0},{"key":"2019-09-13","documentCount":80000},{"key":"2019-09-14","documentCount":0},{"key":"2019-09-15","documentCount":0},{"key":"2019-09-16","documentCount":0},{"key":"2019-09-17","documentCount":0},{"key":"2019-09-18","documentCount":270204},{"key":"2019-09-19","documentCount":0},{"key":"2019-09-20","documentCount":1},{"key":"2019-09-21","documentCount":0},{"key":"2019-09-22","documentCount":0},{"key":"2019-09-23","documentCount":269836},{"key":"2019-09-24","documentCount":0},{"key":"2019-09-25","documentCount":0},{"key":"2020-01-15","documentCount":0},{"key":"2020-01-16","documentCount":0},{"key":"2020-01-17","documentCount":0},{"key":"2020-01-18","documentCount":0},{"key":"2020-01-19","documentCount":0},{"key":"2020-01-20","documentCount":0},{"key":"2020-01-21","documentCount":0},{"key":"2020-01-22","documentCount":0},{"key":"2020-01-23","documentCount":0},{"key":"2020-01-24","documentCount":0},{"key":"2020-01-25","documentCount":0},{"key":"2020-01-26","documentCount":0},{"key":"2020-01-27","documentCount":0},{"key":"2020-01-28","documentCount":0},{"key":"2020-02-09","documentCount":0},{"key":"2020-02-10","documentCount":56000},{"key":"2020-02-11","documentCount":500}];
const margin = {top: 20, right: 20, bottom: 60, left: 60},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
const x = d3.scaleBand().range([0, width]).paddingInner(0.1).paddingOuter(0.5);
const y = d3.scaleLinear().range([height, 0]);
const svg = d3.select("div#histogramHolder").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(data.map(function(d) { return d.key; }));
y.domain([0, d3.max(data, function(d) { return d.documentCount; })]);
// append the rectangles for the bar chart
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.key); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.documentCount); })
.attr("height", function(d) { return height - y(d.documentCount); });
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(60)")
.style("text-anchor", "start");
// add the y Axis
svg.append("g").call(d3.axisLeft(y));
}
}
I would expect the bars to be displayed in steelblue colour as i am setting it ( .attr("class", "bar") ) in the code . however it is not happening
appreciate any help
Any content that is added in DOM by library then customized css of dynamically added element will not applied if it is in component's css. So it's necessary to add customized css in Global style.
Try to put style of svg in your globle style styles.scss

Proper image orientation in AngularJS 1.5 project

Landscape images taken from iPhone appear upside down on Desktop Chrome, but proper orientation on iPhone Safari. An example is the car on the homepage https://www.dapidi.com/#/
I doubt the problem is with the browsers or devices. After searching for an answer I came across this directive which seems to work most times, but not for the image of this car. I believe this should be a "solved problem" already, but I cannot seem to find an answer. Anyone have a solution how to solve this surely common dilemma?
DIRECTIVE
angular.module('myApp')
.directive('imgOrientation', function(){
return {
restrict: 'A',
link: function(scope, element/*, attrs*/) {
function setTransform(transform) {
element.css('-ms-transform', transform);
element.css('-webkit-transform', transform);
element.css('-moz-transform', transform);
element.css('transform', transform);
}
var parent = element.parent();
$(element).bind('load', function() {
EXIF.getData(element[0], function() {
var orientation = EXIF.getTag(element[0], 'Orientation');
var height = element.height();
var width = element.width();
if (orientation && orientation !== 1) {
switch (orientation) {
case 2:
setTransform('rotateY(180deg)');
break;
case 3:
setTransform('rotate(180deg)');
break;
case 4:
setTransform('rotateX(180deg)');
break;
case 5:
setTransform('rotateZ(90deg) rotateX(180deg)');
if (width > height) {
parent.css('height', width + 'px');
element.css('margin-top', ((width -height) / 2) + 'px');
}
break;
case 6:
setTransform('rotate(90deg)');
if (width > height) {
parent.css('height', width + 'px');
element.css('margin-top', ((width -height) / 2) + 'px');
}
break;
case 7:
setTransform('rotateZ(90deg) rotateY(180deg)');
if (width > height) {
parent.css('height', width + 'px');
element.css('margin-top', ((width -height) / 2) + 'px');
}
break;
case 8:
setTransform('rotate(-90deg)');
if (width > height) {
parent.css('height', width + 'px');
element.css('margin-top', ((width -height) / 2) + 'px');
}
break;
}
}
});
});
}
};
});
User error. I had a typo in the directive attribute name in the HTML.
Was: img-fix-orientation
Should have been: img-orientation
(face palm)
Forgot I had renamed it at some point.
The directive seems to work.

How can I CSS transform an Ionicon?

I'm trying to build a bouncy-arrow directive that can be positioned and rotated in markup.
angular.module('directives').directive("bouncyArrow", function ($compile) {
return {
replace: true,
restrict: 'E',
template: '<span class="ion-arrow-right-c"></span>',
scope: {
heading: '='
},
link: function (scope, element, attrs) {
element.css('transform', 'rotate('+attrs.heading+'deg)');
element.css('-webkit-transform', 'rotate('+attrs.heading+'deg)');
element.css('background-color', 'red');
}
};
});
This appears in my template:
<bouncy-arrow heading="15"></bouncy-arrow>
The arrow with red background color appears correctly. But, the transform has no effect. How can I apply the transform to the ionicon?
UPDATE: Here is the fix...
angular.module('directives').directive("bouncyArrow", function () {
return {
replace: true,
restrict: 'E',
template: '<span class="ion-arrow-right-c"></span>',
scope: {
heading: '=',
scale: '=',
positionx: '=',
positiony: '='
},
link: function (scope, element, attrs) {
var transforms = [
'translate(' + attrs.positionx+'px,'+attrs.positiony + 'px)',
'scale(' + attrs.scale + ')',
'rotate(-'+attrs.heading+'deg)',
];
var css = {
selector: '.bouncy-arrow:before',
rules: [
'transform: '+transforms.join(' '),
'-webkit-transform: '+transforms.join(' ')
]
};
var sheet = css.selector + ' {' + css.rules.join(';') + '}';
angular.element(document).find('head').prepend('<style type="text/css">' + sheet + '</style>');
element.addClass('bouncy-arrow');
}
};
});
You need to apply the transform to the :before pseudo class of the "ion-arrow-right-c"
My suggestion is you create a custom class for your element and then use css to do the transform. Much cleaner that way.

ReactJS change background image dynamically?

I was trying to change background image style dynamically for the following div:
Here is my component for changing it,
render: function() {
var divImage = {
backgroundImage : "url(" + this.state.song.imgSrc + "),url(" + this.state.nextImgSrc + ");"
};
return (
<li>
<div ref="image-pane" className="player" style={divImage}></div>
</li>
)
}
Thanks for the help
You haven't specified when would you like to change backgroundImage, so I've created version which changes it with onClick:
React.createClass({
getInitialState: function () {
nextImg: false,
},
handleClick: function () {
this.setState({ nextImg: !this.state.nextImg })
},
render: function() {
var imgUrl = this.state.nextImg ? this.state.nextImgSrc : this.state.song.imgSrc;
var divStyle = {
backgroundImage: 'url(' + imgUrl + ')'
}
return (
<li>
<div ref="image-pane" style={divStyle} onClick={this.handleClick} className="player"></div>
</li>
)
}
});
Notice that backgroundImage: 'url(' + imgUrl + ')' no longer must have trailing semicolon, in fact the trailing semicolon will cause React to raise and error.
This is caused by the trailing semicolon in your style. See react issues #2862.

JCrop selection on image problem in IE

I am using JCrop to crop an image after upload with Ajax upload
control in my web application. It works great on Chrome, Firefox but
not in IE. I use JCrop v0.9.12 (build: 20130202) and IE
v10.0.9.9200.16635. Problem is JCrop selection doesn't work in IE.
Thanks!
Here is my scripts.
<script type="text/javascript">
jQuery(document).ready
(function ($) {
// To hold the API and image size.
var jcrop_api, boundx, boundy;
$('#<%=imgCrop.ClientID%>').Jcrop (
{ // img_crop is the ID of image control
onChange: updatePreview, // will display the selected img on change.
onSelect: updatePreview, // will display the selected img Img_preview
onSelect: storeCoords, // will tell the coordinates
aspectRatio: 11 / 15
}, function ()
{
jcrop_api = this;
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
}
);
function updatePreview(c) {
if (parseInt(c.w) > 0) {
var rx = 100 / c.w;
var ry = 100 / c.h;
$('#<%=Img_preview.ClientID%>').css({ //Img_preview is the ID of image control
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
};
});
// will store the selected part the images coordinates
function storeCoords(c) {
jQuery('#<%=W.ClientID%>').val(c.w);
jQuery('#<%=H.ClientID%>').val(c.h);
jQuery('#<%=X.ClientID%>').val(c.x);
jQuery('#<%=Y.ClientID%>').val(c.y);
};
</script>
A common fix for IE is to do
$.Jcrop('#<%=imgCrop.ClientID%>', {
// your inits
});

Resources