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()功能到那,那麼你可以從任何控制器訪問

相關問題