1

Im a starter in Angularjs and learn it now. I got a web application where i want to add a password to a list. The data came from a database. At the start of the project i got the data in an json file and not on the database. After i chaged (data on database) i got the following error.

i got the following code:

$scope.savePassword = function () {
    if ($scope.currentPassword.Id == null) {
        $scope.passwords.push($scope.currentPassword);
        $scope.isShownEdit = false;
    } else {
        var index = $scope.passwords.findIndex(function (item) {
            return item.Id == $scope.currentPassword.Id;
        });
        $scope.passwords[index] = $scope.currentPassword;
        $scope.isShownEdit = false;
    }
}

Here i got the data which should be saved.

<div>
     <!-- Name des Passwortes -->
     <div class="cClearFloat cInputSpace">
         <input placeholder="Name" maxlength="12" ng-model="currentPassword.Name">
     </div>
     <!-- Passwort des Passwortes -->
     <div class="cClearFloat cInputSpace">
         <input placeholder="Passwort" maxlength="12" ng-model="currentPassword.Passwort">
     </div>
     <!-- Beschreibung des Passwortes -->
     <div class="cClearFloat cInputSpace">
         <input placeholder="Beschreibung" maxlength="25" ng-model="currentPassword.Beschreibung">
     </div>
     <!-- Liste mit den kategorien -->
     <div class="cClearFloat cInputSpace">
         <div class="divSmall">
             <label class="inputDropdownPlaceholder">Kategorien</label>
                 <div class="divOptions" ng-repeat="item in categories.Items">
                     <label class="labelDropDown"><input type="checkbox" class="inputDropDown" ng-model="item.isChecked" ng-checked="checkedCategory(item)" ng-init="item.isChecked = checkedCategory(item)" ng-change="changedCategory(item)">{{item.Name}}</label><br>
                 </div>
         </div>
               <div class="cClearFloat cButtons">
                   <button class="cButtonSpeichern" ng-click="showAlertPassword()">Speichern</button>
                   <button class="cButtonAbbrechen" ng-click="isShownEdit = false">Abbrechen</button>
              </div>
     </div>
</div>

check if all fields got an input

    $scope.showAlertPassword = function () {
    if ($scope.currentPassword.Name && $scope.currentPassword.Passwort && $scope.currentPassword.Beschreibung && $scope.currentPassword.Category.length) {
        $scope.savePassword();
    } else {
        alert("Bitte füllen Sie alle Felder korrekt aus!", "Fehler");
    }
}

I implement the data from the database like this:

$scope.passwords = hateoasService.call('http://localhost:20670/passwords');

When i start the application and want to add the data with the "Speichern" click i get the error.

[error image]

I dont know why i get this error can someone help me?

0

3 Answers 3

1

you used currentPassword instead of $scope.currentPassword,

Change currentPassword instead of $scope.currentPassword and you are done

Then you might be reffering $scope.current_password hash without declaring it.

initialize $scope.current_password = {} at your controller starting.

$scope.savePassword = function () {
    if ($scope.currentPassword.Id == null) {
        $scope.passwords.push($scope.currentPassword);
        $scope.isShownEdit = false;
    } else {
        var index = $scope.passwords.findIndex(function (item) {
            return item.Id == $scope.currentPassword.Id;
        });
        $scope.passwords[index] = $scope.currentPassword;
        $scope.isShownEdit = false;
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

i modified it but the error is the same. this error i got because i modified something and forget to wrote it ;).
take $scope.currentPassword = {} at starting of controller
the $scope.currentPassword = {} code i got in another controller (global).
what is there in password.js line 46
this is the push call $scope.passwords.push($scope.currentPassword);
|
0

You are pushing currentPassword to the array when elsewhere in your code you are referencing $scope.currentPassword which are two unique references to objects.

Changing it to this should resolve your error: $scope.passwords.push($scope.currentPassword);

Comments

0

You have to first initialize it, then after you can push element into it like :

$scope.passwords = [] // in case of array, | do {} in case of json.

$scope.savePassword = function () {
    if ($scope.currentPassword.Id == null) {
        $scope.passwords.push($scope.currentPassword);
        $scope.isShownEdit = false;
    } else {
        var index = $scope.passwords.findIndex(function (item) {
            return item.Id == $scope.currentPassword.Id;
        });
        $scope.passwords[index] = $scope.currentPassword;
        $scope.isShownEdit = false;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.