angularjs - Access Angular scope of controller in nested partial -
in angular project each page has controller. include partial used on multiple pages. in each controller initialize values access them inside partial.
$scope.checkeditems = [];
the in partial bind data it.
<input type="checkbox" checklist-model="$parent.$parent.$parent.$parent.$parent.checkeditems" checklist-value="item.id">
the partial nested have use $parent multiple times access parameter within controller's scope. ugly. also, partial included in different places on different pages, scope depth isn't consistent. therefore on pages need go 5 levels, 8 levels, 4 levels, etc.
is there easier way access controller's scope? it'd great if following worked.
<input type="checkbox" checklist-model="$controllerscope.checkeditems" checklist-value="item.id">
create 1 service data exchange between controllers
nested tree option
in service create object parent->child structure, in nested template store unique id tell service store data in nested tree
in context of template clean logic transferred service
so in template have like:
<input type="checkbox" ng-checked="checked">
and in controller:
$scope.uniqueid='3je99ehr0jer';//generate something... $scope.checked=false; yourservice.set($scope.uniqueid,$scope.$parent.uniqueid,$scope.checked,function(checked){ $scope.checked=checked; }); //where first arg controller unique id , second parent controller uniqueid $scope.$watch('checked',(...notify service change data in tree...));
when want collect full tree use yourservice.getall()
the tree structure shoul like:
scope.datatree={ '3je99ehr0jer':{checked:true,children:[ nodes have parentid 3je99... ]} }
to add/change datatree use recursive function find ids need
yourservice.set = function serviceset(currentid,parentid,data,onchange){ //find recursively parentid if not null // update value // notify parent node (or all) controllers has changed if need (onchange argument) }
flat object option
if want flat object item.id->value content simplier because instead of datatree in yourservice can use data like:
scope.data= { '3je99ehr0jer':false, '03kefkeo0efe':true, ... }
now don't need recursively find ids want scope.data object scope.data['3je99ehr0jer']
i'am lazy test approach figure out how correct something, i'm not sure @ point need notify yourservice
has changed $watch
method , if onchange
method in yourservice
needed figure out this, concept seems correct -> service data exchange based on unique id
if find bugs tell me in comment can edit answer
Comments
Post a Comment