我使用$ rootscope来调用一个事件。$ broadcast和$ scope。$触发多次。 这是我的代码。 controllerA:
$scope.setView =function(){
service.hideView();
}
在服务中:
var confirm = $mdDialog.confirm()
.title('clic ok or cancel')
.targetEvent()
.ok('OK')
.cancel('Cancel');
confirm._options.template = '<md-dialog md-theme="{{ dialog.theme }}" aria-label="{{ dialog.ariaLabel }}" ng-class="dialog.css">' +
' <md-dialog-content class="md-dialog-content" role="document" tabIndex="-1" style="order:0 !important">' +
' <h2 class="md-title">' +
' {{ dialog.title }}' +
' </h2>' +
' <div ng-if="::dialog.mdHtmlContent" class="md-dialog-content-body"ng-bind-html="::dialog.mdHtmlContent"></div>' +
' <div ng-if="::!dialog.mdHtmlContent" class="md-dialog-content-body">' +
' <p>{{::dialog.mdTextContent}}</p>' +
'</div>' +
'</md-dialog-content>' +
'<md-dialog-actions>' +
' <md-button ng-if="dialog.$type == \'confirm\'"ng-click="dialog.abort()" class="md-primary">{{ dialog.cancel }}</md-button>' +
' <md-button ng-click="dialog.hide()" class="md-primary" md-autofocus="dialog.$type!=\'confirm\'">{{ dialog.ok }}</md-button>' +
' </md-dialog-actions>' +
'</md-dialog>';
$mdDialog.show(confirm).then(function() {
$rootScope.$broadcast("eventFired",{});
}, function() {
});
在controllerB中:
$scope.$on("eventFired",function(){
$scope.hideTemplate();
});
在控制器B中,它调用了hideTemplate 3次。如何解决这个?