Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent AngularJS from making lowercase HTML attributes

Tags:

angularjs

svg

I am trying to render some SVG with AngularJS but I can't dynamically change the viewbox of the svg element.

Angular renders a 'viewbox' attribute, but browsers expect a 'viewBox' attribute. So the result is:

<svg height="151px" width="1366px" viewBox="{{ mapViewbox }}" viewbox="-183 425 1366 151">

How can I get the result I expect:

<svg height="151px" width="1366px" viewBox="-183 425 1366 151">

Thanks.

like image 930
lud Avatar asked Jan 30 '13 00:01

lud


People also ask

How do you lowercase in AngularJS?

lowercase() Function. The angular. lowercase() Function in AngularJS is used to convert the string into lowercase. It can be used when the user wants to show the text in lowercase instead of uppercase or one wants to compare two strings.

What is attrs in AngularJS?

Using attrs you are able to access the attributes defined in your html tag like <fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true"> So in this case you will have access to the symbol and readonly attributes.

What is restrict in AngularJS directive?

Restrict. Angular allows us to set a property named restrict on the object we return on our directive definition. We can pass through a string with certain letters letting Angular know how our directive can be used. function MyDirective() { return { restrict: 'E', template: '<div>Hello world!

How to use directive AngularJS?

Create New Directives In addition to all the built-in AngularJS directives, you can create your own directives. New directives are created by using the . directive function. To invoke the new directive, make an HTML element with the same tag name as the new directive.


2 Answers

See if this directive works:

app.directive('vbox', function() {
  return {
    link: function(scope, element, attrs) {
      attrs.$observe('vbox', function(value) {
        element.attr('viewBox', value);
      })
    }
  };
});

HTML:

<svg height="151px" width="1366px" vbox="{{ mapViewbox }}">

Plnkr. You'll need to "Inspect element" or "View source" to see the svg tag.

Update: If your app includes jQuery, see Does the attr() in jQuery force lowercase?

@Niahoo found that this worked if jQuery is included (he made an edit to this post, but for some reason, other SO moderators rejected it... I liked it though, so here it is):

 element.get(0).setAttribute("viewBox", value);
like image 143
Mark Rajcok Avatar answered Jan 27 '23 21:01

Mark Rajcok


Looking for a solution myself I came upon this answer the correct way to achieve this is by using pattern transform ng-attr-view_box

like image 37
probinson Avatar answered Jan 27 '23 22:01

probinson