What is happening here is some menu items are enabled and some are disabled in the ui. So when user clicks on a menu item which is disabled it gives an error on the page.
I tried to solve the issue using angularjs-service and $broadcast. Now the thing is since every page including the left menu has different controllers so i needed to repeat the $broadcast data to every controller. All I am looking forward to is how can I remove this redundancy?
Left-Menu-Service.js
'use strict';
angular.module("MainApp")
.factory('menuClick', function($rootScope) {
var sharedService = {};
sharedService.notify = {};
sharedService.prepForBroadcast = function(msg) {
this.broadcastItem();
};
sharedService.broadcastItem = function() {
$rootScope.$broadcast('handleBroadcast');
};
return sharedService;
});
Left-Menu-Controller.js
'use strict';
angular.module("MainApp")
.controller('LeftMenuCtrl', function ($scope, $rootScope, menuClick) {
$scope.handleMenuClick = function(action) {
menuClick.notify.warningNotify = true;
menuClick.notify.errorNotify = true;
menuClick.notify.successNotify = true;
if(!action.IsEnabled)
{
menuClick.notify.warningNotify = false;
menuClick.notify.warningMessage = "This operation is disabled ( "+action.Text+" )";
menuClick.prepForBroadcast(menuClick.notify);
}
};
});
Left-Menu.html
<li>
<a ng-click="handleMenuClick(submenu)">{{submenu.Text}}</a>
</li>
Notification-Directive.js
'use strict';
angular.module("MainApp")
.directive('notification', function () {
return {
restrict: 'E',
templateUrl: function (tElement, tAttrs) {
if (tAttrs.type) {
switch (tAttrs.type){
case 'error':
return 'partials/template/show_error.html';
break;
case 'success':
return 'partials/template/show_success.html';
break;
case 'warning':
return 'partials/template/show_warning.html';
break;
}
}
}
};
});
show_error.html
<div ng-hide="notify.errorNotify" ng-init="notify.errorNotify=true">
<button type="button" ng-click="notify.errorNotify = !notify.errorNotify"></button>
<h2>{{notify.errorMessage}}</h2>
</div>
Controller-of-the-all-the-pages-where-this-directive-is-used.js
$scope.$on('handleBroadcast', function() {
$scope.notify = menuClick.notify;
});
I don't know how patch the whole stuff in the directive itself so that repetition of the above code in all the controllers could be avoided. Thanks!