简体   繁体   中英

AngularJS update doesn't work

I have a problem when updating my guitar object in my AngularJS app. It runs on an ngMock backend

The guitar object is updated with the updateGuitar controller. Then the carService will process the put request. The problem lies with getting the updated object in my carView controller, so when I do location.path(/car/view/id or something I get the updated object.

Here's my guitarView controller:

window.app.controller('guitarViewCtrl', ['$scope', '$routeParams', '$location', 
'GuitarService', function ($scope, $routeParams, $location, GuitarService) {
 'use strict';

     $scope.guitarId = $routeParams.guitarId;
     initGuitar($scope.guitarId);

     function initGuitar(guitarId) {
       GuitarService.showDetails(guitarId).then(function success(guitar) {  
           $scope.guitar = guitar;            
       }, function error(response) {
       });
     }
}]);

My GuitarService: Someone said I need to populate my Guitars in this service (I now do this in the mock.js) but I don't know how I should do this.

window.app.service('GuitarService', ['HTTPService', '$http', function (HTTPService, $q, $http) {
'use strict';

    this.showDetails = function (opleidingsprofielId){

      // HTTP service returns correct urls
      return HTTPService.get('/opleidingsprofiel/view/' + opleidingsprofielId);

     this.put = function (opformUpdate, opleidingsprofielId) {
         var deferred = $q.defer();
          $http.put('/#/opleidingsprofiel/:opleidingsprofielId/update', opformUpdate)
             .then(function resolve(response){
                 deferred.resolve(response.data);
              }, function reject(response){
                 deferred.reject(response);
              });
            return deferred.promise;
           };
     }]);

Few problems right off the bat... the this.put line wouldn't be called how the code is right now since there's no closing brace } to finish the showDetails function and the return from that function happens before it would get to the this.put line. In your variable injection with the array you are missing '$q' you should see some errors in the console about $http as is.... make sure the code you pasted is the code your running or ideally put the code up in a "working" as best you can plnkr or codepen.

Template

this is some fake code because sometimes SO is dumb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM