簡體   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