javascript - Dynamically adding watchers to inputs -
i'm making dynamic form renderer gets list of fields database , renders them user can input data , send database.
right im adding functionality set evaluations fields have options, making give number base evaluation depending on option select user.
/* load fields database result looks $scope.fields = [{field_name: "dishes", input_type: "radio", options:["{number: 20, value: 'tuna'}", "{number: 30, value:'salmon'}"], id: "id1", is_evaluated: true }] know watcher wont dict info that's why keep option dicts strings (i dont remember correct jsonparse syntax assume wrote dicts correctly) */ watchers = []; for(field in $scope.fields){ if($scope.fields[field].is_evaluted == true){//there can fields don't have numeric evaluation watchers.push($scope.$watch(function(){ return $scope.values[$scope.fields[field].id]; }, function(){ console.log("watcher triggered"); console.log("new value is:" + json.parse($scope.values[$scope.fields[field].id]).value) }); } }
where $scope.values dict it's keys field ids keeps options selected, option value needed database processing , "number" numeric evaluation used html display(those console.logs jquery/angular code display evaluation) note: there can field not evaluated
everything seems till try application , well, last evaluated field watched (like add 9 evaluated fields, 3 non evaluated fields , watchers trigger on same field)
note2: saw dynamically add $watch question gave me idea, sadly couldn't use exact save code 2 reasons 1: use array , use dict, 2: care input value while want see change can use other value
so should change? why watchers last variable instead of 1 wanted give them? appreciated
my html this:
<div ng-repeat="field in fields"> <ng-if="field.input_type == 'radio'"> <ng-repeat="option in field.options"> <input type="radio" value={{option}} ng-model="$scope.values[field.id]">{{option.value}}</ng-if> </ng-repeat> </ng-if> </div>
there lot more html in this...(like each type of input radio perfect example evaluated fields) render 2 radio inputs 1 saying tuna , other saying salmon, when choose salmon want display 30 points , when switch tuna want display 20
Comments
Post a Comment