It's annoying that camel cased attribute names are unable to pass data
I know the angular convention specifies that html attribute names should be dashed (IE some-attribute-in-html) and that these attributes are automatically converted to camel case when they are accessed by angular, such as when using an attribute within a directive.
I agree this is a good thing and the way things ought to be done. Sometimes though, for whatever reason either ignorance or preference, users will want to declare camel cased attribute names. Take for instance the directive
<new-directive someAttr="some value"> </new-directive>
Inside the javascript when working with this directive, for instance inside the link function, one would expect to still be able to access this value. IE You would expect the following code to print to the console "someAttr value: some value".
app.directive('newDirective', function () {
return {
restrict: 'E',
link: function(scope, element, attrs){
console.log("someAttr value: " + attrs.someAttr)
}
};
});
However, this doesn't work, since someAttr evaluates to undefined within the javascript. Once again, although I recognize this isn't best practice I see no reason why camel cased attributes declared in the HTML shouldn't still work. It's bad practice true, but it seems overly paternalistic to make it completely impossible to declare in the HTML such camel cased attribute names. Or if that rankles you too much to be enabling such bad practice, at the very least Angular should print an error to the console telling you that your attribute name was unable to be processed.
HTML is case-insensitive, this means <someDirective> could refer to either "someDirective" or "somedirective", whereas now it unambiguously points to "somedirective".
As @jussik points out, the DOM parser is case insensitive, so it provides AngularJS with a canonical form, e.g. SOMEDIRECTIVE or somedirective, but it does not hold on to the specific casing you provided. This is why you cannot use camelCase to identify attribute or element in the attrs param.
Huh, good point. Thanks for the response Pete and Jussik