【问题标题】:AngularJS update doesn't workAngularJS 更新不起作用
【发布时间】:2016-06-16 18:18:40
【问题描述】:

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

吉他对象使用 updateGuitar 控制器进行更新。然后 carService 将处理 put 请求。 问题在于在我的 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) {
       });
     }
}]);

我的吉他服务: 有人说我需要在此服务中填充我的吉他(我现在在 mock.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;
           };
     }]);

【问题讨论】:

  • 对不起,我更新了我的代码,应该已经编辑了帖子本身。上次。

标签: javascript angularjs


【解决方案1】:

很少有问题马上出现... this.put 行不会被调用,因为没有右大括号 } 来完成 showDetails 函数,并且该函数的返回发生在它之前到 this.put 行。在使用数组进行变量注入时,您缺少“$q”,您应该在控制台中看到一些关于 $http 的错误。确保您粘贴的代码是您正在运行的代码,或者理想情况下将代码放在尽可能“工作” plnkr 或 codepen。

Template

this is some fake code because sometimes SO is dumb

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多