H
H
HolmesInc2016-01-04 15:35:25
Angular
HolmesInc, 2016-01-04 15:35:25

How to compare elements in $scope?

Good afternoon. So we have a form with the fields name, email, password. They are stored (or contained, not sure how exactly) by the newUser model. it looks something like this:

<input type="text" class="form-control" id="regUserName" name="regUserName" placeholder="Ваше имя" ng-model="newUser.name" required/>
<input type="email" class="form-control" id="regUserEmail" name="regUserEmail" placeholder="Email" ng-model="newUser.email" required/>
<input type="password" class="form-control" id="regUserPassword" name="regUserPassword" placeholder="Пароль" ng-model="newUser.password" required/>
<button style="float:right;" class="btn btn-primary" name="signUp" ng-disabled="regForm.$invalid || ConfirmPass(newUser)" ng-click="SignUp(newUser)">Поехали!</button>

When submitting the form, we run the SignUp() function. It contains the following:
$scope.SignUp = function(newUser){
    $http.get('php_scripts/check_user.php').success(function(data){
      $scope.dbInfo = data;
    });

Our php script works and all data from dbInfo can be displayed via ng-repeat
(by the way, in data we get data in the form [{"email":"[email protected]"},{"email":"[email protected] ua"}]they are entered in dbInfo.)
So the question is - I need to compare the email from the form (that is, newUser.email) with those stored in dbInfo.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
HolmesInc, 2016-01-07
@HolmesInc

I solved the problem a little differently: indeed, if you try to read the elements from the received JSON as $scope.dbInfo[i], then it will give an error. That is the condition

if(newUser.email==$scope.dbInfo[i]){
-----
}

won't work. To fix it, just add a prefix of the elements received in JSON. In my case, the JSON was: [{"email":"[email protected]"},{"email":"[email protected]"}]
So the correct condition would be with the addition of the email prefix:
if(newUser.email==$scope.dbInfo[i].email){
-----
}

S
Sergey, 2016-01-04
Protko @Fesor

we have one object, we have another object .... we compare using js.

K
kto_to, 2016-01-06
@kto_to

If you don't want everything by hand, you can use Underscore/Lodash:
'emaiIsExist' will be undefined if there is no object in the array of objects that has the given field with the given value. Otherwise - will contain the first matching object from the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question