在下面,我有一个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 :(得分:0)
您可以使用$controller
服务从主控制器调用您的leftNav控制器,如下所示
angular.module('myApp').controller('main', function($scope,$http, myFactory,$controller) {
$http.post(...blablabl..).then(function() {
var leftCTRL= $controller('leftNav', { $scope: $scope.$new() });
});
});
答案 1 :(得分:0)
你可以尝试这样:
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;
});
});