-
Notifications
You must be signed in to change notification settings - Fork 27.5k
"invalid value for attribute" errors at startup for svg elements #1050
Comments
Is there a directive or another Angular technique to get around this error? Ng-cloak isn't sufficient. |
Sadly, I've run into this exactly same issue. Presumably Chrome is "evaluating" the SVG before AngularJS has a chance to go in and mess with the DOM? (and rightfully cannot evaluate the string "<[ 100+v.row*100 ]>" down to a number with Angular's help). |
I have had the same problem. Finally, I decided to write my own directives: angular.module('yourmodule.directives', [])
.directive('ngX', function() {
return function(scope, element, attrs) {
scope.$watch(attrs.ngX, function(value) {
element.attr('x', value);
});
};
}) Then, use <circle ng-x="{{ x }}" y="0" r="5"></circle> Finally got rid of annoying error messages. |
That is an excellent solution. I will try it out. However, I wonder if I will have to create a whole bunch of directives for all the attributes that are affected. Thanks. |
Similarly to ngHref and ngSrc you can do angular.forEach(['x', 'y', 'width', 'height'], function(name) {
var ngName = 'ng' + name[0].toUpperCase() + name.slice(1);
myModule.directive(ngName, function() {
return function(scope, element, attrs) {
attrs.$observe(ngName, function(value) {
attrs.$set(name, value);
})
};
});
}); |
Thanks for sharing that snippet, @rkirov, super helpful! Strange problem though: If you use that for "d" attributes as well, and try to set the value to empty string, resulting in |
Yes. |
Sometimes is not desirable to use interpolation on attributes because the user agent parses them before the interpolation takes place. I.e: <svg> <circle cx="{{cx}}" cy="{{cy}}" r="{{r}}"></circle> </svg> The snippet throws three browser errors, one for each attribute. For some attributes, AngularJS fixes that behaviour introducing special directives like ng-href or ng-src. This commit is a more general solution that allows prefixing any attribute with "ng-attr-", "ng:attr:" or "ng_attr_" so it will be set only when the binding is done. The prefix is then removed. I.e: <svg> <circle ng_attr_cx="{{cx}}" ng-attr-cy="{{cy}}" ng:Attr:r="{{r}}"></circle> </svg> Closes angular#1050 Closes angular#1925
Sometimes is not desirable to use interpolation on attributes because the user agent parses them before the interpolation takes place. I.e: <svg> <circle cx="{{cx}}" cy="{{cy}}" r="{{r}}"></circle> </svg> The snippet throws three browser errors, one for each attribute. For some attributes, AngularJS fixes that behaviour introducing special directives like ng-href or ng-src. This commit is a more general solution that allows prefixing any attribute with "ng-attr-", "ng:attr:" or "ng_attr_" so it will be set only when the binding is done. The prefix is then removed. I.e: <svg> <circle ng_attr_cx="{{cx}}" ng-attr-cy="{{cy}}" ng:Attr:r="{{r}}"></circle> </svg> Closes angular#1050 Closes angular#1925
Great solution vsirisanthana. I was having the same error "Expected moveto path command ('M' or 'm') angular" when drawing a line for the same reason on the attribute 'd'.
Many thanks |
Actually, these days (AngularJS 1.5) you prepend your attributes with "ng-attr-", so:
would become:
|
about the problem Error: Invalid value for attribute x1="{{viewBox.x1}}" you can replace the x1 and y1 by When using ngAttr, the allOrNothing flag of $interpolate is used, so if any expression in the interpolated string results in undefined, the attribute is removed and not added to the element |
It's working with replacing d with ng-attr-d |
j8-attr-* 속성이 왜 필요하냐 하면 요런 문제 때문이다. angular/angular.js#1050 앵귤러가 해결한 방법을 그대로 따라간다.
Thanks peterjhart , it worked. I was wondering would it work if I created a link function and then processed the component through $onInit. |
Using chrome Version "20.0.1132.27 beta" (but also seen on other versions)
For this html (obviously it's simplified from the original):
it draws the expected rectangle, but when I look in the javascript console I see:
Error: Invalid value for attribute x="{{0}}" bug.html:7
Error: Invalid value for attribute y="{{0}}" bug.html:7
Error: Invalid value for attribute width="{{100}}" bug.html:7
Error: Invalid value for attribute height="{{100}}" bug.html:7
that apparently come from before the values are evaluated. They seem harmless, but are distracting (the real code generates a couple of thousand such errors, due to ng-repeat -- see http://dave.whipp.name/tutorial/anti-alias.html).
The text was updated successfully, but these errors were encountered: