0

In the below, I have a sidenav and a main content section. I am trying to call the sidenav function from the main controller so the number updtes in the sidenav. What am I doing wrong here?

This is my view.

<div>Teams {{ teams.length }} </div>

This is my sidebar controller:

angular.module('myApp').controller('leftNav', function($scope, myFactory) {
  myFactory.getTeams().then(function(res) {
    $scope.teams = res.data;
  })

This is my factory

angular.module('myApp').factory('myFactory', function($http) {

  return {
    getTeams : function() {
        return  $http.get('/teams').then(function(res) {
        return res;
      });
    }
  };
});

This is a complete different controller:

 angular.module('myApp').controller('main', function($scope,$http, myFactory) {
     $http.post(...blablabl..).then(function() {
         // What can i call here to update the sidenav above?
    });
});
Mark
  • 2,061
  • 1
  • 16
  • 26
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • look at this post :http://stackoverflow.com/questions/25417162/how-do-i-inject-a-controller-into-another-controller-in-angularjs – Bowen Li Jul 14 '16 at 05:17

2 Answers2

0

You can utilise $controller service to call your leftNav controller from your main controller like below

angular.module('myApp').controller('main', function($scope,$http, myFactory,$controller) {
     $http.post(...blablabl..).then(function() {
        var leftCTRL= $controller('leftNav', { $scope: $scope.$new() });
    });
});
Rishi Tiwari
  • 1,041
  • 1
  • 10
  • 20
0

you can try like so :

angular.module('myApp').controller('leftNav', function($scope, myFactory) {
  myFactory.getTeams().then(function(res) {
    this.teams = res.data;
})



angular.module('myApp').controller('main', function($scope,$http, $controller) {
      var leftNav= $controller('leftNav');

     $http.post(...blablabl..).then(function(res) {
         leftNav.teams = res.data;
    });
});
Bowen Li
  • 387
  • 1
  • 8