0

I'm clicking a table row to edit the fields in a modal. The modal must have 2 functionalities (Add or Edit) depending on the GET request data like below.

$scope.editInterview = function(id) {
    $http.get('/api/getinterview/' + id).then(function(response) {
        editedObject = response.data.item
}

HTML

    <label ng-if="editedObject.email">{{editedObject.email}}</label>
    <label ng-if="!editedObject.email">Email</label>

    <input ng-model="newObject.email" />

enter image description here

I am able to display the object in the labels, but that's not much help, because the data needs to be shown in the input boxes to be Edited and Saved.

How can i show the data from editedObject.email in the input, so i can save it using newObject.email?

I tried ng-init="editedObject.email", but it doesn't work. Is there some other ng-something that does this or i should be doing it in another way?


Update:

Edit and Update Methods, both are in the mainController.

$scope.editInterview = function(id) {
    $http.get('/api/getinterview/' + id).then(function(response) {
        editedObject = response.data.item
    })
}

//Controller for the Modal
function DialogController($scope, $mdDialog, editedObject) {

    $scope.editedObject = editedObject

    $scope.submitObject = function(newObject) {
        $http.post('/api/interview', newObject)
    }
}

1 Answer 1

1

You have to make a deep copy from editObject.email to newObject.email. This could be done this way in controller after editOject.email has a value assigned.

$scope.newObject.email = angular.copy($scope.editObject.email);
Sign up to request clarification or add additional context in comments.

3 Comments

I updated the question with the Edit Button function editInterview , and the function that the Modal is using. The editedObject is being assigned on the GET request, but the newObject is comming from the view. How can i di this if the newObject is only set in the view?
If I get it right you want to set the $scope.newObject.email which is assigned as model in the input field to be equal to the $scope.editObject.email. If this is the purpose then the way to go is the one I present on my Updated answer. If not you have to describe the problem in more detail.
Yes the $scope! This got me so twisted for half a day, that i started having bad thoughts xD I didn't know about angular.copy, need to do some more reading about this. Thank you so moch for the help Korte!

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.