1

I am new to AngularJS and i was trying out AngularJS Behaviors with custom element attributes. I have this weird situation when i changed the name of the attribute i created. My initial code was like this.

HTML

<div ng-app="myApp">
    <div enter="panel" leave>Content</div>
</div>

JavaScript

app.directive('enter', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('mouseenter', function() {
                element.addClass(attrs.enter); // <- I have put a breakpoint here for inspection
            });
        }
    }
});

app.directive('leave', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('mouseleave', function() {
                element.removeClass(attrs.enter);
            });
        }
    }
});

As you can see the attribute name that triggers the behavior is enter and this works as expected by adding the class panel to the div. (The breakpoints is hit when debugging) But the problem occurs when i try to change the name of the attribute to a more meaningful one.

Now the modified code..

HTML

<div ng-app="myApp">
    <div onEnterAddCss="panel" leave>Content</div>
</div>

JavaScript

app.directive('onEnterAddCss', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('mouseenter', function() {
                element.addClass(attrs.onEnterAddCss); // <- Breakpont here is never hit.
            });
        }
    }
});

app.directive('leave', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('mouseleave', function() {
                element.removeClass(attrs.onEnterAddCss);
            });
        }
    }
});

Now when i change the attribute name to onEnterAddCss this does not work. The breakpoint never get hit. I am confused. Is there a specific character length that an attribute can be? Please help.

3 Answers 3

2

As with all other attributes with Angular (such as directives), you need to changed it to lower cases and dashes:

<div ng-app="myApp">
    <div on-enter-add-css="panel" leave>Content</div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

This is due to how angular normalized attribute names.

https://docs.angularjs.org/guide/directive scroll down to Normalization.

It is essentially looking for an attribute named on-enter-add-css

Comments

1

It is related to camelCase, in html your attribute : on-enter-add-css not onEnterAddCss

angular doc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.