javascript - Checkbox selection is lost when using ng-init -


i want initialize(check) checkbox once based on value.hence using ng-init problem when load template , come previous template again think ng-init gets call , loose new checkbox selection.

code :

$scope.lst = [ {item:'banana', selected:true}, {item: 'apple', selected:false}, {item:'milk', selected:false}, {item: 'tomato', selected:false}, {item:'juice', selected:false} ] }); <div ng-repeat="item in lst">      <input ng-model="item.parentselected" ng-init="item.parentselected=item.selected" type="checkbox"> </div> 

so default display banana selected problem when user select apple , milk , load template , user comes same page(again load first template) guess ng-init gets call again , loose apple , milk selection.

when remove ng-init have apple , milk selection.

update: guess problem ng-init because ng-model binded parentselected , apple , milk have selected false when select apple , milk , load template , again load previous template ng-init making selection of banana because of ng init="item.parentselected=item.selected"

how make default selection of apple , preserve other selection when templates gets load??

it gets lost because when open again view, controller gets init again, , previous selection gets lost because view init again zero.

define service store selection, inject in controller , store inside service selected value. define function inside controller called ng-init. inside function check if value has been stored: if so, assign value stored one, otherwise pre select first entry.

i hope makes sense


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -