javascript - Angular $broadcast called inside ng-repeat fires multiple times -
i have nested forms in ng-repeat button click event calls function couple of things including $scope.$broadcast. in function triggers once except broadcast triggers on per item in ng-repeat.
i cannot move button outside repeat or lose form , data references. have demo plunker shows basic setup. if enter values 1 of inputs , click next while monitoring console , should see broadcast fire multiple times (once per ng-repeat item). need way have broadcast fire once while still maintaining reference form validation checking per demo code.
here js , rest in plunker:
(function () { var app = angular.module('app', []), /** manually triggers $validate event validate given form */ isformvalid = function ($scope, ngform) { var = null; //$scope.$emit('$validate'); $scope.$broadcast('$validate'); if(! ngform.$invalid) { return true; } else { // make form fields '$dirty' validation messages shown ngform.$dirty = true; for(i in ngform) { if(ngform[i] && ngform[i].hasownproperty && ngform[i].hasownproperty('$dirty')) { // todo: 'field.$invalid' test required? ngform[i].$dirty = true; } } } }; app.controller('appctrl', ['$scope', function ($scope) { $scope.wizardstep = 1; $scope.nextstep = function () { var ngform = $scope['stepform_' + $scope.wizardstep]; if(isformvalid($scope, ngform)) { // trigger manual validation $scope.wizardstep++; } }; $scope.prevstep = function () { $scope.wizardstep--; }; $scope.submit = function () { var ngform = $scope['stepform_' + $scope.wizardstep]; // can make line common if(isformvalid($scope, ngform)) { alert('form valid. submitting...'); } }; }]); })();
any or ideas appreciated , know things styles in html , other aspects of demo code no-no's. did plunkr faster.
tia
the event fired once. problem testctrl beeing used 3 times it's catching event 3 times.
you need create separate controllers each step. hack debouncing $on handler if must, create separate controllers logic in common service. or maybe base controller , child controllers (inheritance between them)
Comments
Post a Comment