0

HTML:从外部控制器调用ng-view控制器方法?

<html ng-app="san"> 
<head> 
</head> 
<body ng-controller="sanctrl"> 
<div> 
    AngularJS</div> 
<div ng-view=""> 
</div> 
<button type="button" ng-click="init()" class="btn btn-blue"> 
    Save</button> 
</body> 
</html> 

主控制器:

san.controller('sanctrl', function ($scope, $http, $q, $location, $anchorScroll, $filter, dataFactory) { 
//nothing in this controller. 
}); 

NG-视图控制器:

san.app.controller('cereals', function ($scope, dataFactory) { 
    $scope.init(){ 
    $scope.test = "test"; 
    }; 
}); 

我试图调用从外部纳克视点范围init方法。可能吗?如果不是,那么我们有什么选择?

回答

1

您可以使用您所触发的广播事件:

$rootScope.$broadcast('testEvent', testArray); 

然后在第2个控制器,你可以监听的事件:

$scope.$on('testEvent', function(event, array) { 

     // Do stuff here 

    }); 
+0

真棒:) :)为我工作 –

+0

伟大的东西! :) – lulu88

0

正如@ lulu88你可以使用$broadcast比其他做

写一个单独的服务移动代码int()功能到那,那么你可以从任何控制器访问

相关问题