0

Hey guys I wonder if there's a solution on this mess I normally create in Angular projects:

app.controller('indexController', function ($scope) {
    scope.hideWinkelContainer = true;
    scope.hideWinkelPaneel = true;
    scope.headerCart = false;
    scope.showCart = function () {
        scope.hideWinkelContainer = false;
        scope.hideWinkelPaneel = false;
    };
    scope.hideCart = function () {
        scope.hideWinkelContainer = true;
        scope.hideWinkelPaneel = true;
    };
});

html:

<div class="containerWinkelwagen" ng-hide="hideWinkelContainer"> <div class="winkelWagenPaneel" ng-hide="hideWinkelPaneel">
    <div class="winkelWagenTerug" ng-click="hideCart()"></div>
    <div class="winkelWagenTerug" ng-click="showCart()"></div>
 </div> 
</div>

Best practices, tips, examples are always welcome!

1
  • What is wrong with what you have written ? Are you looking for some optimisation ? Don't forget to keep it simple for maintainability (lel angular maintainability)! Commented Aug 12, 2015 at 13:16

2 Answers 2

2

You can simply use a toggle function as follow:

app.controller('indexController', function ($scope) {
    $scope.hideWinkelContainer = true;
    $scope.hideWinkelPaneel = true;
    $scope.headerCart = false;

    $scope.toggleCart = function () {
        $scope.hideWinkelContainer = !$scope.hideWinkelContainer;
        $scope.hideWinkelPaneel    = !$scope.hideWinkelPaneel;
    };
});

In your HTML:

<div class="containerWinkelwagen" ng-hide="hideWinkelContainer">
    <div class="winkelWagenPaneel" ng-hide="hideWinkelPaneel">
        <div class="winkelWagenTerug" ng-click="toggleCart()"></div>
        <div class="winkelWagenTerug" ng-click="toggleCart()"></div>
    </div> 
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

You can implement the show/hide functions once in a factory, and then inject it into the controllers that need it. Saves a lot of boilerplate.

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.