2016-07-14 3 views
0

以下では、sidenavとメインのコンテンツセクションがあります。私はメインコントローラからsidenav関数を呼び出そうとしているので、sidenavの番号が更新されます。私はここで間違って何をしていますか?別のコントローラからの角度表示の更新に関する問題

これは私の見解です。

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

これは私のサイドバーのコントローラです:

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

これは、これは完全に別のコントローラである私の工場

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

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

です:

angular.module('myApp').controller('main', function($scope,$http, myFactory) { 
    $http.post(...blablabl..).then(function() { 
     // What can i call here to update the sidenav above? 
    }); 
}); 
+0

見ます。http:// stackoverflowの.com/questions/25417162/how-do-i-inject-a-controller-another-controller-in-angularjs –

答えて

0

あなたはそうのように試すことができ

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

以下のようなあなたのメインコントローラからあなたleftNavコントローラを呼び出すために$controllerサービスを利用することができます

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; 
    }); 
}); 
この記事で
+0

ポストのデータが自分のAPIのチームを反映していません – KingKongFrog

+0

コードを編集しました。再び? –

関連する問題