Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show a div on a button click using angularjs and HTML

Tags:

html

angularjs

I want to show a div on a button click using angularjs and HTML I am learning angularJs. I want to design a webpage in which on click of 'Add' button a div(EmployeeInfoDiv) is shown. When I fill the textboxes in it and save it the data should reflect in table within the div(EmpDetTable).

Here is the HTML page code:

<body ng-app="MyApp">
 <div ng-controller="EmpDetCtrl">       
    <div ng-model="EmpDetTable" class="container body"  ng-hide="addEmp">

        <label class="TabHeadings">Employee Details</label>
        <div class="EmployeeInfo">

            <table ng-model="Employee" border="1">
                <thead><tr><th>Name</th><th>Designation</th><th>Salary</th></tr></thead>
                <tbody>
                    <tr ng-repeat="emp in employees">
                        <td>{{emp.name}}</td>
                        <td>{{emp.desg}}</td>
                    </tr>
                </tbody>
            </table>
            <button ng-model="AddEmp" ng-click="ShowAddEmployee()">Add</button> 
        </div>
      </div>
    <div ng-model="EmployeeInfoDiv" class="popover right EmployeeInfo" style="width:1500px;"  ng-show="addEmp" >
     <label class="TabHeadings" style="width:830px;">Employee Details</label>

    <div class="EmployeeInfo">
            <table>
                <tr><td><label class="table_label">Name:</label></td><td><input type="text" ng-model="name" class="textbox"/></td>
                <td><label  class="table_label">Designation:</label></td><td><input type="text" ng-model="desg" class="textbox"/></td>
                </tr>

            </table>
            <div ng-model="botton_container">
                <button ng-model="save" class="save_buttons" ng-click="SaveData();">Save</button> 
                <button ng-model="cancel" class="cancel_buttons"  ng-click="ShowViewEmployee();">Cancel</button>
            </div> 

        </div>
   </div>

</div>  
</body>

Controller Code:

 function EmpDetCtrl($scope) {

   $scope.employees = [
   { name: 'A',desg: 'C'}

   ];

   $scope.addNew = function () {
    $scope.employees.push({
        name: $scope.name,desg: $scope.desg
     });
    }

  $scope.ShowAddEmployee= function () {
    return $scope.EmployeeInfoDiv=true;
   }

  $scope.ShowViewEmployee=function () {
      return $scope.EmployeeInfoDiv = false;
  }

}

I don't know whether this code is correct or not. I just tried it. Can anyone please help me out. Thanks in advance.

like image 434
Harabati Avatar asked Jun 24 '26 15:06

Harabati


1 Answers

Your ng-click is updating the EmployeeInfoDiv variable. So, reference that in ng-show and ng-hide:

<div ng-hide="EmployeeInfoDiv" class="container body">
...
<div ng-show="EmployeeInfoDiv" class="popover right EmployeeInfo" style="width:1500px;">

You do not need ng-model in the div to make that work. ng-model="EmployeeInfoDiv"

Update
A few more issues. Most important is that (at least in the code posted) MyApp isn't defined anywhere, and your controller is defined on the global scope. So you need to change your html to:

<div ng-app>
<div ng-controller="EmpDetCtrl"> 
...

However, note that this is not the recommended method. See the angular documentation for controllers.

Here is a working demo: http://jsfiddle.net/wittwerj/xrB77/

like image 95
j.wittwer Avatar answered Jun 27 '26 08:06

j.wittwer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!