【问题标题】:AngularJS using $resourceAngularJS 使用 $resource
【发布时间】:2016-05-20 13:21:49
【问题描述】:

我有一个问题,我使用具有 GET 和 POST 方法的服务。我尝试更新视图中的选择组件。因此,当我尝试添加新项目并单击按钮时,该按钮会触发新对象的 POST,而我想要的是使用我的 GET 方法接收更新的列表,但它不起作用。仅当我刷新页面时才有效。我猜是回调有问题

这是我使用的代码:

控制器

$scope.addSubject = function(){
    var newSubject = {"subjectName" : $scope.subjectType};
    InterpelationSubjectFactory.create(newSubject);
    /* Calling query method to update subjectType list */
    InterpelationSubjectFactory.query(function(response){
      $scope.subjectTypes =  response;
    });
    console.log($scope.subjectTypes);
    //$scope.selectedSubjectType =  $scope.subjectType;
    $scope.hideSubjectForm = true;
    $scope.subjectType = '';
    /*console.log(newSubject);*/
  }

服务

services.factory('InterpelationSubjectFactory', function($resource){ 
return $resource(baseUrl + '/subjectTypes', {}, {
   query: { method: 'GET', isArray: true},
   create: { method: 'POST'}
 })
});

请有人指出我做错了什么吗? 谢谢

【问题讨论】:

    标签: angularjs service callback resources controllers


    【解决方案1】:

    对于回调,您只有一个,以防成功。你能添加一个处理错误并显示错误消息的吗?这应该给我们一个线索。

    InterpelationSubjectFactory.query(function(response){
        // success handler
        $scope.subjectTypes =  response;
    }, function(error) {
        // error handler
        console.log("Error InterpelationSubjectFactory.query: " + JSON.stringify(error));
    }
    );
    

    请分享错误信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多