I have one controller to get all records from table1. In table2, I have a foreign key to table1. With machineController I managed to populate select tag in html. But want on post to fill sparePartController. From some reason this is not working, I made a mistake somewhere, could you help me understand where?
<div ng-app="app" ng-controller="sparePartController as vm">
<div class="form-group" ng-controller="machineController as machineVM">
<label>Machine Type</label>
<select class="form-control" ng-model="vm.newSparepart.machine" ng-options="machine.name for machine in machineVM.machines"></select>
</div>
</div>
//sparePartController.js
(function () {
"use strict";
angular.module("app")
.controller("sparePartController", sparePartController);
function sparePartController($http)
{
var vm = this;
vm.spareParts = [];
vm.newSparePart = {};
vm.errorMessage = "";
vm.isBusy = true;
$http.get("/spares/getAll")
.then(function (response) {
//success
angular.copy(response.data, vm.spareParts);
}, function (error) {
vm.errorMessage = error;
}).finally(function () {
vm.isBusy = false;
});
vm.addSparePart = function () {
vm.isBusy = true;
vm.errorMessage = "";
$http.post("/spares", vm.newSparePart)
.then(function (response) {
alert("Test");
vm.spareParts.push(response.data);
vm.newSparePart = {};
}, function () {
alert(vm.data);
vm.errorMessage = "failed to save new spare";
}).finally(function () {
vm.isBusy = false;
});
};
}
})();
// machineController.js
(function () {
"use strict";
angular.module("app")
.controller("machineController", machineController);
function machineController($http) {
/* jshint validthis:true */
var vm = this;
vm.machines = [];
vm.newMachine = {};
vm.errorMessage = "";
vm.isBusy = true;
$http.get("/machines/GetMachines")
.then(function (response) {
// Success
angular.copy(response.data, vm.machines);
}, function (error) {
// Failure
vm.errorMessage = "Failed: " + error;
}).finally(function () {
vm.isBusy = false;
});
vm.addMachine = function () {
vm.isBusy = true;
vm.errorMessage = "";
$http.post("/machines", vm.newMachine)
.then(function (response) {
vm.machines.push(response.data);
vm.newMachine = {};
}, function () {
//fail
vm.errorMessage = "Failed to save new machine type";
}).finally(function () {
vm.isBusy = false;
});
};
}
})();
Can you please check this, where am I wrong ? Code to check