javascript - Angular - Controller with no HTML and broadcasted events -
i have weird situation. have controller want control when dialog opens. i'm using ngdialog in combination templates , directives. here dialogcontroller code:
(function() { 'use strict'; angular.module('common').controller('dialogcontroller', dialogcontroller); dialogcontroller.$inject = ['$scope', 'ngdialog']; function dialogcontroller($scope, ngdialog) { $scope.$on('opendialog', open); function open(which) { ngdialog.open({ template: which, classname: 'newproductdialog' }); } } })();
this controller has no html associated - sole purpose open, close, etc ngdialog.
here how try trigger dialog open (vm.navigate
called through ng-click) in homecontroller:
function initnav() { /** * nav items array. * @type {{name: string, id: string, selected: boolean, icon: string}[]} */ vm.navs = [{ name: 'home', id: 'home', selected: true, icon: 'ss-home' }, { name: 'new', id: 'create-new-product', selected: false, icon: 'ss-addfile' }]; /** * color of navigation elements. * @type {string} */ vm.color = '#ea8daf'; /** * navigation function when clicking nav item. * @param item {object} $scope.navs object clicked. */ vm.navigate = function(item) { switch(item.id) { case 'create-new-product': ----->$rootscope.$broadcast('opendialog', 'newproduct'); break; } }; }
and associated html above method: <ft-nav items="homectrl.navs" onselect="homectrl.navigate" color="homectrl.color"></ft-nav>
this doesn't work though. i'm wondering if it's because dialogcontroller
has no associated html? doing wrong broadcast event? know can use service , dialogcontroller
can watch service, using $broadcast seems better way go. ideas?? thanks!
you correct. dialogcontroller
won't run if it's not specified on dom element using ngcontroller
directive.
instead of defining controller, use .run
block.
app.run(function ($rootscope, ngdialog) { /* ... */ });
the code passed .run
run right away without associated dom.
Comments
Post a Comment