繁体   English   中英

AngularJS更新不起作用

[英]AngularJS update doesn't work

在AngularJS应用中更新吉他对象时遇到问题。 它在ngMock后端上运行

吉他对象使用updateGuitar控制器更新。 然后,carService将处理放置请求。 问题在于在我的carView控制器中获取更新的对象,所以当我执行location.path(/car/view/id或其他东西时,我获取了更新的对象。

这是我的guitarView控制器:

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) {
       });
     }
}]);

我的GuitarService:有人说我需要在此服务中填充我的Guitars(我现在在模仿文件js中这样做),但我不知道该怎么做。

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;
           };
     }]);

马上就解决了一些问题...由于没有结束括号},因此无法立即调用this.put行,因为没有结束括号}可以完成showDetails函数,并且该函数的返回在到达this之前发生.put行。 在使用数组进行变量注入时,您缺少“ $ q”,您应该在控制台中看到一些有关$ http的错误。...确保粘贴的代码是您正在运行的代码,或者理想情况下将代码放在您可以通过plnkr或Codepen来“尽其所能”。

模板

this is some fake code because sometimes SO is dumb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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