javascript - Angular JS 1.5 - I want to communicate between angular js components -
i trying expose deletephoto
, editphoto
methods of businessphotos
components verticalgrid
component. how not accessible.
any appreciated, thanks
photo.js
angular.module('business') .component('businessphotos', { templateurl: 'business/photos.html', controller: [ function() { var $ctrl = this; $ctrl.editphoto = function(photo) { // code }; $ctrl.deletephoto = function(id) { // code }; }] });
photo.html
<vertical-grid ng-if="$ctrl.business.provider_photos" cells="$ctrl.business.provider_photos" delete-photo="$ctrl.deletephoto(id)" edit-photo="$ctrl.editphoto(photo)"></vertical-grid>
vertical-grid.js
angular.module('dashboard') .component('verticalgrid', { bindings: { cells: '<', deletephoto: '&', editphoto: '&' }, templateurl: 'utils/vertical-grid.html', selector: 'vertical-grid', controller: ['$element', function($element) { var $ctrl = this; $ctrl.colgrid = []; var numofcols = 4; var numofcells = $ctrl.cells.length; $ctrl.colcssclass = 'col-xs-3'; (var = 0; < numofcells; i++) { var colindex = % numofcols; if (!$ctrl.colgrid[colindex]) { $ctrl.colgrid[colindex] = []; } $ctrl.colgrid[colindex].push($ctrl.cells[i]); } }] });
vertical-grid.html
<div class="provider-photos row"> <div class="{{$ctrl.colcssclass}}" ng-repeat="col in $ctrl.colgrid track $index"> <div class="photo-outer-tile" ng-repeat="cell in col track $index"> <div class="photo-tile"> <img ng-src="{{cell.tile_url}}"> <div class="photo-controls"> <a ng-click="$ctrl.deletephoto(cell.id)" href="javascript:void(0);">delete</a> <a href="javascript:void(0);">crop</a> </div> </div> </div> </div> </div>
to communicate between angularjs controllers use service
(or factory
, conceptually similar).
see example here.
can propagate events in real-time service controllers using $watch
concept.
a simpler way use inject $rootscope
in both controllers.
Comments
Post a Comment