0

i want to fetch data from the 2 controllers. im getting data from one controller but im not getting data from the other controller.how to call the two controllers in one function

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get("url")
    .then(function(response) {
        $scope.pir_data= response.data;
        $pir_Status_bit_= response.data.pir_staus;

        app.controller('watertankCtrl', function($scope, $http) {
            $http.get("url")
            .then(function(response) {
                $scope.watertank_data = response.data;
                $scope.wt_level = response.data.dataFrame;   
            });
        });

html

<div class="col-lg-4" ng-app="myApp" ng-controller="myCtrl">
    <div class="our-team-main">
        <div class="team-front">
            <h4>Door Monitoring</h4>
            <a>PIR Smart Security</a>
            <hr />
            <p align="left">
                Door Status-  {{pir_Status_bit}} <br />
            </p>
        </div>
    </div>
</div>

<div class="col-lg-4" ng-app="myApp" ng-controller="watertankCtrl">
    <p align="left">
        Level - {{wt_level}} v<br />          
    </p>
</div>

</div>
</div>
georgeawg
  • 48,608
  • 13
  • 72
  • 95
krishna mohan
  • 23
  • 1
  • 4
  • 1
    If your data needs to come from multiple controllers it might be worth looking into turning one of them into a service instead. – Jhecht Jul 05 '19 at 08:05

1 Answers1

0

A clean way to handle something like this would be using the $q service and using the all method. What's happening is that one of your $http.get promises is returning before the other. You want to wait for both to return.

Warren
  • 1
  • 1
  • 1