how to call a html file from a controller in angularjs?

All we need is an easy explanation of the problem, so here it is.

I am new to angularjs. I know how data can be passed between a controller and view and vice-versa.

In node.js, we can render html files with the data using express.js.

Is it possible to do the same with angularjs?

For example:

$scope.edit = function(id) {
    EmployeeService.editEmployee(id).then(function (response) {
        if(!response) {
            console.log("no response");
        } else {
            console.log(response.data);
        }
    }, function (err) {
        if(err) {
            console.log(err);
        }
    });
};

In the above logic, I need to render “edit.html” file with the response.data.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can simply use the ng-include directive to achieve this:

index.html

<html>
  <head>
    <script src="lib/script.js"></script>
  </head>

  <body ng-app="myApp" ng-cloak>
    <div ng-controller="MainCtrl">
      <div id="all-employees" ng-show="!selectedEmployee">
        <table>
          <thead>
            <tr>
              <td>ID</td>
              <td>Name</td>
              <td>Age</td>
              <td>Options</td>
            <tr>
          </thead>
          <tbody>
            <tr ng-repeat="employee in employees">
              <td>{{ $index }}</td>
              <td>{{ employee.name }}</td>
              <td>{{ employee.age }}</td>
              <td><button ng-click="edit($index)">Edit</button></td>
            <tr>
          </tbody>
        </table>
      </div>

      <div id="selected-employee" ng-hide="!selectedEmployee">
        <div ng-include="'views/edit.html'"></div>
        <button ng-click="selectedEmployee = undefined">Back</button>
      </div>
    </div>
  </body>
</html>

lib/script.js

angular.module('myApp', []).controller('MainCtrl', function($scope, EmployeeService) {

  $scope.employees = [
    {name: 'test', age: 24},
    {name: 'guest', age: 29},
    {name: 'fest', age: 39}
  ];

  $scope.edit = function(index) {
    EmployeeService.editEmployee(id).then(function (response) {
        if(!response) {
          console.log("no response");
        } else {
          $scope.selectedEmployee = response.data;
        }
    }, function (err) {
        if(err) {
          console.log(err);
        }
    });
  };

  $scope.unsetSelection = function() {
    $scope.selectedEmployee = undefined;
  };
});

views/edit.html

<div>
  <h3>Employee Detail</h3>
  <div>{{ selectedEmployee.name }}</div>
  <div>{{ selectedEmployee.age }}</div>
</div>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply