Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button alignment relative to button inside of another controller

Tags:

html

angularjs

I have two buttons inside separate controllers.

<div ng-controller="aCtrl">
  <button class="addButton" ng-click="toggle()"> Add </button>
  <form ng-hide="myVar" ng-submit="submit()">
    <input ......
    <input ......
  </form>
</div>

<div ng-controller="bCtrl">
  <button class="EditButton" ng-click="toggle()"> Add </button>
  <form ng-hide="myVar" ng-submit="submit()">
    <input ......
    <input ......
  </form>
</div>

Note: Toggle just switches the hide/show bool in the back-end

As you can see when clicking the Addbutton it will show the form for aCtrl and EditButton for bCtrl. The result of the current layout is when Add Buttons form expands it pushes the EditButton down. I don't think this can be fixed with CSS as its the logical flow of the HTML.

I am looking for solutions that would allow me to have the buttons at the top in the flow of the page then the forms below.

for example I tried:

  <button ng-controller="aCtrl" class="EditButton" ng-click="toggle()"> Add </button>
  <button ng-controller="bCtrl" class="addButton" ng-click="toggle()"> Add </button>

<div ng-controller="aCtrl">
  <form ng-hide="myVar" ng-submit="submit()">
    <input ......
    <input ......
  </form>
</div>

<div ng-controller="bCtrl">
  <form ng-hide="myVar" ng-submit="submit()">
    <input ......
    <input ......
  </form>
</div>

Which doesn't seem to work.

like image 675
gxminbdd Avatar asked Jan 18 '17 06:01

gxminbdd


People also ask

How do I align a button to the right in a container?

If you want to move the button to the right, you can also place the button within a <div> element and add the text-align property with its "right" value to the "align-right" class of the <div>.

How do you align buttons on the same line?

If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.

How do you align buttons?

We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.


1 Answers

The problem is that ng-hide hides the content with a display: none that causes the space occupied by the element to collapse.
You need visibility: hidden that also hides the element, but keeps the space.

Therefore, use ng-class instead of ng-hide:

<div ng-controller="aCtrl">
  <button class="addButton" ng-click="toggle()"> Add </button>
  <form ng-class="{ 'hidden' : myVar }" ng-submit="submit()">
    <input ......
    <input ......
  </form>
</div>

<div ng-controller="bCtrl">
  <button class="EditButton" ng-click="toggle()"> Add </button>
  <form ng-class="{ 'hidden' : myVar }" ng-submit="submit()">
    <input ......
    <input ......
  </form>
</div>

and the CSS

.hidden {
    visibility: hidden;
}

Here is a live sample:

var myApp = angular.module('myApp',[]);

function aCtrl($scope) {
    $scope.myVar = true;
    $scope.toggle = function () {
      $scope.myVar = !$scope.myVar;
    }
}

function bCtrl($scope) {
    $scope.myVar = true;
    $scope.toggle = function () {
      $scope.myVar = !$scope.myVar;
    }
}
.hidden {
  visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="myApp">
  <div ng-controller="aCtrl">
    <button class="addButton" ng-click="toggle()"> aCtrl.Add </button>
    <form ng-class="{ 'hidden' : myVar }" ng-submit="submit()">
      <input type="text" value="aCtrl.form">
    </form>
  </div>

  <div ng-controller="bCtrl">
    <button class="EditButton" ng-click="toggle()"> bCtrl.Add </button>
    <form ng-class="{ 'hidden' : myVar }" ng-submit="submit()">
      <input type="text" value="bCtrl.form">
    </form>
  </div>
</section>

As you can see, the bCtrl.Add button remains in place, regardless whether aCtrl.form is visible or not.
like image 131
Jose Rui Santos Avatar answered Sep 29 '22 11:09

Jose Rui Santos