javascript - Stop a For Loop From Repeating in AngularJS -
i'm trying replace html code javascript when user searches email. have working correctly, reason error displays around 20+ times, replace div ,
"user existuser existuser existuser existuser existuser exist"
instead of putting error message once. idea how can fix it?
$scope.checkemail = function findusersmatchingemail(emailaddress) { ref.child('users').orderbychild('email'). equalto($scope.emailaddress).once('value', function (snap) { var output = '<div>', myerror = document.queryselectorall('#d'); (var key in arguments[0]) { output += (snap.name() + (snap.val() === null ? ' not' : ' does') + ' exist'); } output += '</div>'; (var = myerror.length - 1; >= 0; i--) { myerror[i].innerhtml = output; } }); };
as wrote in comments, never manipulate dom anywhere link function of directive. explaining why out of scope answer, , has been answered multiple times here on so. read is:
so make short, here's simple demo of how handle error messages.
note no means nor best way it. there's ngmessages
or form validation in general used well.
(function (app) { 'use strict'; app.controller('emailctrl', ['$scope', function ($scope) { $scope.errors = []; $scope.checkemail = function findusersmatchingemail(emailaddress) { // clear previous errors $scope.errors.length = 0; // check email , add errors if needed // using service (ref) $scope.errors.push({ message: 'email not unique ' + (math.random()) // random used show errors change }); }; }]); })(angular.module('app', []));
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script> <div data-ng-app="app" data-ng-controller="emailctrl"> <ul> <li data-ng-repeat="error in errors">{{ error.message }}</li> </ul> <form data-ng-submit="checkemail(email)"> <input data-ng-model="email" placeholder="email"> </form> </div>
side note: use controller as
syntax, though did not want introduce more new topics.
Comments
Post a Comment