AngularJS에서 두 개의 다른 컨트롤러간에 통신 할 수 있는지 알고 싶습니다. http://plnkr.co/edit/if0MQwlx9WHrD8XnMi2t?p=previewAngularJs에서 두 개의 다른 모듈의 컨트롤러간에 통신하는 방법
1. var app = angular.module('fistModule', []);
app.controller('first', function ($scope) {
$scope.firstMethod= function() {
//my code}
})
2. var newapp = angular.module('secondModule,[]');
newapp.controller('second', function ($scope) {
$scope.secondMethod= function() {
//my code}
두 개의 서로 다른 모듈의 컨트롤러간에 통신하는 방법이 있나요 : 나는 두 모듈,
Plunker 있다고 가정합시다.
내 코드 : JS :
angular.module('myApp', [])
.controller('ParentCtrl', ['$scope',
function($scope) {
$scope.message = "Child updated from parent controller";
$scope.clickFunction = function() {
$scope.$broadcast('update_parent_controller', $scope.message);
};
}
]);
angular.module('myNewApp', [])
.controller('ChildCtrl', ['$scope',
function($scope) {
$scope.message = "Some text in child controller";
$scope.$on("update_parent_controller", function(event, message) {
$scope.message = message;
});
}
])
HTML :
<div ng-app="myApp" ng-controller="ParentCtrl">
<div ng-app="myNewApp" ng-controller="ChildCtrl">
<p>{{message}}</p>
</div>
<button ng-click="clickFunction()">Click</button>
</div>
합니다. –
이것은 $ 브로드 캐스트를 사용하는 컨트롤러간에 통신 할 수 있음을 의미합니다. –
네, 그 일을하는 데 아무런 문제가 없습니다. –