Here is my form's select control:
<select ng-model="truckMfg.truckMfgs" ng-options="truckMfg.Name for truckMfg in truckMfgs">
<option value="" selected>Select Truck Manufacturer ..</option>
</select>
I have a modal that allows the user to add Mfgs not already in the table. Here is my controller:
function TruckMfgCtrl($scope, $routeParams, $http, API, $location) {
$scope.truckMfgs = API.GetTruckMfgs(); // <-- this works at initialization
$scope.save = function () {
$http.post('/api/TruckMfgs/', $scope.truckMfg) // data does get saved corectly
.success(function () {
})
.error(function (data) {
alert(data);
});
setTimeout(function () {
$scope.truckMfgs = API.GetTruckMfgs(); // <-- this is not working
$scope.$apply();
}, 1000);
};
}
What I am trying to accomplish:
After the user adds a new manufacturer and the modal closes, I want the select control to be updated to the new list of manufacturers. The addtl mfg is being added to the table but it is not appearing in the select control. Refreshing the form will show the new data but that of course is not what I want.
Any ideas?
Mark Rajcok
I appreciate your help. I tried this and it did not work:
$scope.save = function () {
$http.post('/api/TruckMfgs/', $scope.truckMfg)
.success(function () {
$scope.truckMfgs.append($scope.truckMfg);
console.log($scope.truckMfg);
})
.error(function (data) {
alert(data);
});
};
Respectfully, could you show me the code you are recommending? Thanks
(PS: Checked out your profile, I too drink cawfee and take my dawghter to the mawl, but not in Nork) :)
I don't see any need to call GetTruckMfgs() in save(), because you already have the data you need to add to your truckMfgs list. So instead, have save() add/append $scope.truckMfg to $scope.truckMfgs. This way you avoid another trip to the server.
You could do this in your success() callback, if you want to ensure that the data was successfully post'ed before you update your select list. If you want your UI to update faster (i.e., be more responsive), you could do this before (or after) the post() call in your save function, but then handle any errors in your error callback (e.g., remove the item from the select list if post() failed).