Sharing AngularJS models -
i have angular controller canvascontroller modelling canvas onto "widgets" can placed.
the controller maintains simple array, _currentwidgets, of object literals correspond widgets on canvas. e.g. [ {id: 1} ].
a canvasdirective tied canvascontroller, angular watch placed on array currentwidgets in link function. means canvas re-rendered when widgets added , removed.
the canvas template contains following line render widgets:
<div ng-include="'widget-template'" ng-repeat="widgetmodel in currentwidgets" /> and widget template defined follows:
<div ng-controller="widgetcontroller widgetctrl">foo</div> it understanding means whenever new item added currentwidgets new instance of widgetcontroller instantiated angular use new widget.
my question - how can share model created in canvascontroller new instance of widgetcontroller instantiated when widget added? or not idiomatic , should instantiate separate model widget inside widgetcontroller?
in case might shared already. see prototypal inheritance. understand widgets should have scopes child canvas directive, right?
a quick solution create method in parent, javascript's prototypal inheritance should lookup method child parent's.
or send events...
the controller's role extend scope, perhaps $rootscope.
that means controller's scope child of $rootscope. can try $rootscope.$broadcast() or scope.$emit(). that's 1 way, sending events.
$emit() send events going upward $rootscope child scope. $broadcast() send events going downward parent descendants.
Comments
Post a Comment