Angular http post z relacją oneToOne

0
html

<!DOCTYPE html>
<html>
<head>
    <style>
        table, th, td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
        }

        table tr:nth-child(odd) {
            background-color: #f1f1f1;
        }

        table tr:nth-child(even) {
            background-color: #ffffff;
        }
    </style>

    <meta charset="UTF-8">
    <title>ziom</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="people">

    <table>
        <tr>
            <th>id</th>
            <th>imie</th>
            <th>nazwisko</th>
            <th>pensja</th>
        </tr>
        <tr ng-repeat="x in names">
            <td>{{ x.id}}</td>
            <td>{{ x.firstName }}</td>
            <td>{{ x.lastName}}</td>
            <td>{{ x.salary}}</td>


        </tr>
    </table>
    <p>addresy zamieszkania</p>
    <table>
        <tr>
            <th>id</th>
            <th>miasto</th>
            <th>kod pocztowy</th>
            <th>ulica</th>
            <th>numer domu</th>
        </tr>
        <tr ng-repeat="x in names">
            <td>{{x.id}}</td>
            <td>{{x.address.locality}}</td>
            <td>{{x.address.zipCode}}</td>
            <td>{{x.address.street}}</td>
            <td>{{x.address.streetNumber}}</td>
        </tr>
    </table>

    <table>
        <tr>
            <th>id</th>
            <th>phone</th>
        </tr>
        <tr ng-repeat="x in names">
            <td>{{x.id}}</td>
            <td>{{x.phones }}</td>
        </tr>
    </table>

    <div ng-controller="registerController">
        <form  action="javascript:void(0);">
            <h2>Register Form</h2>
            <div>
                <label>Name</label>
                <input type="text" ng-model="firstName" placeholder="firstName" required="required" firstName="firstName"
                       style='margin-left: 10px;'>
            </div>
            <div>
                <label>lastName</label>
                <input type="text" ng-model="lastName" placeholder="lastName" required="required" lastName="lastName"
                       style='margin-left: 10px;'>
            </div>
            <div>
                <label>Salary</label>
                <input type="text" ng-model="salary" placeholder="salary">
            </div>

            <div>
                <label>Address</label>
                <input type="text" ng-model="address" placeholder="address"
                      >
            </div>
            <div>
                <label>Address</label>
                <input type="text" ng-model="locality" placeholder="address" required="required" locality="locality"
                       style='margin-left: 10px;'>
            </div>

            <button ng-click='Register()' style='margin-top:15px'>Register</button>
        </form>

    </div>

</div>
<script>

    var app = angular.module('myApp', []);
    app.controller('registerController', function($scope, $http) {

        $scope.Register = function() {
            $http.post("http://localhost:8080/employee/add",{'firstName': $scope.firstName,'lastName':
            $scope.lastName,'salary': $scope.salary, 'address': $scope.address.id})
                    .success(function(response) {$scope.usersData = response;$scope.message = response.message;})

            $http.post("http://localhost:8080/address/add",{'locality': $scope.locality,'street':
            $scope.street})
                    .success(function(response) {$scope.usersData = response;$scope.message = response.message;}

                    );

        };
    });



</script>

<script>
    var app = angular.module('myApp');


    app.controller('people', function ($scope, $http) {
        $http.get('http://localhost:8080/employee/employee')
                .success(function (response) {
                    $scope.names = response;
                });
    });




</script>


</body>
</html>

 

w Postmanie tak dodaje nowego Employeer'a z addresem

{

		
    "salary": 3443434,
    "firstName":"Jan",
    "lastName":"343434",
    "address": {
    	"id":1
    }
}
 

Ale nie wiem jak to zrobic w htttp post Angular pomoże ktoś?

0

Cześć,
Oczywiście w post jest delikatna różnica, bo musisz przekazać całą data która jest z formularza :-)
Ogólnie do controllera (powinieneś robić to w innym plik np. service.js -> i dodatkowo stworzyć tam fabrykę, bo kontroler odpowiada tylko za widok), ale to tylko nawiasem.

Więc w kontrolerze tworzysz metodę:

function sendData($scope) {
$http({
url: 'request-url',
method: "POST",
data: { 'message' : message }
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
}

I robisz według tego wzoru. Jeżeli chcesz np. dodać pola z formularza - to domyślam się, że wiesz na jakiej zasadzie działa dodawanie do data rzeczy z pól formularza :-)

0

Dalej nie wiem jak to zrobić

z mojej klasy Employee mogę dodać imię,nazwisko,pensje itp ale jak mam relacje one to one z address class to juz nie wiem jak to zrobic

probowalem :
$scope.address.id
$scope.address._id
$scope.address

pomocy, potrzebuje to do projektu na uczlnie a angulara nie znam w ogóle.

ok w końcu rozwiązałem swój problem temat do zamknięcia.

0

Podzieliłbyś się może rozwiązaniem problemu? Sam jestem ciekaw jak to zrobione jest

1 użytkowników online, w tym zalogowanych: 0, gości: 1