[SOLVED]
I created a simple SVG directive using angular 1.3-beta which features a "type: 'svg'" for directive. The code is straightforward (see Plunker http://plnkr.co/edit/vgElXdWXvfH0faH0qKHk?p=preview - updated with solution):
Create a SVG element containing a square
when the square is clicked, a class is added to it (fill with red)
ISSUE: the class is correctly added to the SVG square element but it is ignored and the square remains black.
Here is the js part:
var app = angular.module('app', [])
.directive('svgDirective', function () {
return {
template: '<svg xmlns:xlink="http://www.w3.org/1999/xlink"> <g ng-transclude> </g> </svg>',
transclude: true,
type: 'svg',
replace: true
};
})
.directive('svgSquare', function() {
return {
template: '<rect width=100 height=100></rect>',
type: 'svg',
replace: true,
link: function (scope, element, attrs) {
element.on('click', function () {
element.addClass("selected");
});
}
};
});
and the CSS:
.selected {
fill: '#f00'; --> SOLUTION: no quotation mark: fill: #f00;
}
Your CSS file contains an error. You specified the fill color in quotation marks where there should be none. Replace the CSS with the following code and your example is working as intended:
.selected {
fill: #f00;
}
Also note that if you use jQuery as your AngularJS backend, adding CSS classes might be impossible (see this question)
Related
I've used this sort of directive before for a fallback image if the image does not load correctly.
app.directive('fallbackSrc', function () {
var fallbackSrc = {
link: function postLink(scope, element, attrs) {
element.bind('error', function() {
angular.element(this).css("background-image", attrs.fallbackSrc);
});
}
}
return fallbackSrc;
});
This works great when it is placed on the html like this, of course the directive would replace the src of the image instead of modifying the css:
<img fallback-src="http://google.com/favicon.ico" ng-src="{{image}}"/>
I currently have a background-image though:
<div class="issue-gallery-container" fallback-src="http://google.com/favicon.ico" style="background-image: url({{ AWS }}images/cover/{{ item.volume.number }}.{{ item.number }}.png)">
</div>
The directive right now does not pick up the error on the element since it occurs in the elements css. How would I modify the directive to listen for an error on the elements background-image?
I would setup a "dummy" img directive that changes its parent css. Or you could create a a template to simplify things even more.
Here is a working plunker http://plnkr.co/edit/334WIH2VUGReVTUYt2Tb?p=preview
Code is a bit messy but it works.
app.directive('backgroundFallbackSrc', function () {
return {
link : function(scope, element, attrs) {
element.bind('error', function() {
element.parent().css('background-image', 'url("' + attrs.backgroundFallbackSrc + '")');
});
}
}
});
html
<div class="issue-gallery-container" style="display:block; height:2000px;">
<img background-fallback-src="http://keithfimreite.com/BlogFiles/keithfimreite/SAAS.jpg"
ng-src="{{invalidImage}}" style="display:none;">
</div>
I'm simply trying to set the width of the dialog box, and I haven't succeeded (yet). I have added a CSS class, but the width set is the one of the dialog shade.
.dialogwidth800 {
width : 800px;
}
...
ngDialog.openConfirm({
template: 'templateRemove',
className: 'ngdialog-theme-default dialogwidth800',
scope: $scope
}).then(
EDIT : fiddle corrected with working solution here : https://jsfiddle.net/jr626fww/8/
It can easily achieved by using custom css like below :
.ngdialog.ngdialog-theme-default.custom-width-800 .ngdialog-content {
width: 800px;
}
.ngdialog.ngdialog-theme-default.custom-width-900 .ngdialog-content {
width: 900px;
}
Use these css class when you open ngDialog :
To use custom-width-900
ngDialog.openConfirm({
template: 'templateRemove',
className: 'ngdialog-theme-default custom-width-800',
scope: $scope
});
To use custom-width-900
ngDialog.openConfirm({
template: 'templateRemove',
className: 'ngdialog-theme-default custom-width-900',
scope: $scope
});
Apply width with class .ngdialog-content{width :100px !important;} Using !important is very important to override any css property
I'm using Angularjs with highcharts-ng library and I want to have a highchart directive with the ability to export the chart (showing the export button) and another without that option (not showing the export button) but I have not managed to disable (hide) the button using the configuration object. How can I do that?
Here is the snippet
var app = angular.module('app', ['highcharts-ng']);
app.directive('myChart', function(){
return {
restrict: 'E',
scope: {},
template: '<highchart config="chartConfig"></highchart>',
link: function(scope, element, attrs) {
scope.chartConfig = {
options: {
exporting: {
enabled: false
}
}
};
}
};
});
<div ng-app="app">
<my-chart></my-chart>
<div>
<script src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="https://code.angularjs.org/1.3.0/angular.js"></script>
<script src="https://rawgit.com/pablojim/highcharts-ng/0.0.7/src/highcharts-ng.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
Even if a disable the export property
var HighChartChartModel = {
options: {
exporting: {
enabled: false
},
chart: {
type: 'bar'
}
},
This will work
Here is the right way to do this in highcharts-ng:
options: {
chart: {
type: 'bar'
},
exporting: {
enabled: false
}
},
Note that exporting is at the same level as chart.
I got the same problem, and since the regular options somehow don't work, I just hide it with some CSS and javascript (jQuery) where needed.
$('.highcharts-button').css('display': 'none');
looks like a bug in highcharts-ng.
if you simply don't include the exporting.js you won't get that button.
a working fiddle here
I'm trying to adding rtl-ltr support for my application .
here is the question : assume that there is input like this :
<span class="sp-right">
<label>
Number:
</label>
</span>
is it possible to change all sp-right class to sp-left programmatically ?
is it a good idea in ltr and rtl support in angular ?
Thanks
With jQuery
$(".sp-right").each(function(){
$(this).removeClass("sp-right").addClass("sp-left");
});
Above code will run once only in code sequence. Put it in event listening function like:
$("button.trigger").click(function(){
$(".sp-right").each(function(){
$(this).removeClass("sp-right").addClass("sp-left");
});
});
Then it will run when event trigger.
Or you can build a directive. I am writing for you
_module.directive("spRight", function () {
return {
restrict: 'C',
link: function (scope, element, attrs) {
element.removeClass("sp-right").addClass("sp-left");
}
};
});
UPDATE:
app.directive("buttonThatTrigger", function () {
return {
restrict: 'C',//target all elements with class button-that-trigger
link: function (scope, element, attrs) {
element.click(function(){
$(".sp-right").each(function(){
$(this).removeClass("sp-right").addClass("sp-left");
});
});
}
};
});
Then add a class in button:
<button class="button-that-trigger"></button>
One more approach. Instead of changing multiple classes on the page (although it's not that difficult), it's much more reasonable to change only one class on the top level of the application container and from there manage all the classes with CSS.
For example:
<span class="sp-direction">
<label>Number:</label>
</span>
In CSS
.sp-direction {
direction: ltr; /* default text direction */
}
.sp-right .sp-direction {
direction: rtl;
}
.sp-left .sp-direction {
direction: ltr;
}
Now all you need to do is to change sp-right/sp-left classes on say body tag:
<body class="sp-{{spClass}}">
And in any controller:
$rootScope.spClass = 'left';
$rootScope.spClass = 'right';
I'm trying to create AngularJS directive that I will use inside svg element.
The directive do not create svg element but use exist one.
I can see the right svg markup in the dev-tools but the browser does not display it.
Please see live example.
This is the directive:
angular.module('ui.directives', []).directive('svgText',
function() {
return {
restrict: 'E',
replace: true,
template: '<text fill="green" x="4" y="20" font-weight="bolder" font-size="2" font-family="Arial">89</text>'
};
}
);
This happens because jQuery (which AngularJS uses under the hood) doesn't know how to create svg elements. You can workaround this by adding a link function to the directive that clones the element that was created but creates it in the SVG namespace.
A potentially better method is to wrap your template in an SVG element (with namespace defined) and then in the link function pull out the child element and use that, it will already be created in the correct namespace.
module.directive(
'svgText',
function () {
return {
restrict: 'E',
template: '<svg xmlns="http://www.w3.org/2000/svg"><text fill="green" x="4" y="20" font-weight="bolder" font-size="2" font-family="Arial">89</text></svg>',
replace: true,
link: function (scope, elem, attrs) {
// Extract the child element to replace the SVG element.
var child = angular.element(elem[0].firstElementChild);
// Copy attributes into element.
for (attrName in attrs) {
if(typeof attrs[attrName] === "string") {
child.attr(attrName, attrs[attrName]);
}
}
elem.replaceWith(child );
}
};
}
);
I have published an article on AngularJS + SVG which talks through many such issues.
http://www.codeproject.com/Articles/709340/Implementing-a-Flowchart-with-SVG-and-AngularJS