-8

this is my script coding for left side menu slide left and right. I want to convert it into angularJS, I copied it from other website and unable to understand.

<script>
    var menuLeft = document.getElementById('cbp-spmenu-s1'), 
        showLeftPush = document.getElementById('showLeftPush'),
        body = document.body;

    showLeftPush.onclick = function() {
        classie.toggle(this, 'active');
        classie.toggle(body, 'cbp-spmenu-push-toright');
        classie.toggle(menuLeft, 'cbp-spmenu-open');
        disableOther('showLeftPush');
    };

    function disableOther(button) {
        if (button !== 'showLeftPush') {
            classie.toggle(showLeftPush, 'disabled');
        }
    }
</script>

1 Answer 1

0

I can see that this code is some sort of toggle functionality. So let me try to explain how we can convert this!

The code starts at the onclick, so in angular that will be ng-click, also in angular you can write this in the HTML like so

<button id="showLeftPush" ng-click="test=!test;" ng-init="test=false;" ng-class="{'active': test, 'disabled': test}"></button>

In the above line, I will initialize test variable to false. then ng-click will toggle this variable. Then you are toggling the active class for the button. So the angular equivalent for that will be ng-class where the test will be a boolean, if test is true, then the class will be added. I do the same thing for disabled class also.

Similarly I add the other classes to the other element like.

Body HTML:

<body ng-class="{'cbp-spmenu-push-toright': test}" ng-controller='MyController' ng-app="myApp">

Div with ID - cbp-spmenu-s1:

<div id="cbp-spmenu-s1" ng-class="{'cbp-spmenu-open': test}">left</div>

Please use this as a starting point and continue building you angular App.

I have included a demo for your reference.

JSFiddle Demo

Sign up to request clarification or add additional context in comments.

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.