2016-11-20 16 views
0

嗨,我写了一个$资源服务来连接api。 这里是我的controller.js

var header = { 
'Content-Type': 'application/x-www-form-urlencoded' 
}; 
var myData = { 
'phone': '12345678' 
}; 
selfApi2.data('/tableName',header,{where:{"name":"kevin"}).update(myData, function(result){ 
console.log("update Kevin's phone succeed",result); 
}) 

它工作在service.js

.factory('selfApi2', function ($resource, localStorageService) { 
var AB = { 
    data: function (apiURL, header, data, params) { 
    return $resource("http://localhost:4000/api" + apiURL, null, { 
     update: { 
     method: 'POST', 
     headers: header, 
     data: data, 
     params: params 
     } 
    }); 
    } 
}; 
return AB; 
}) 

的代码。但为什么变量myData应该放入update()部分而不是data()部分?

回答

0

在你的情况下,data()是将刚刚创建ReST资源这将暴露rest的API get save query remove delete为默认功能。

所以在这个data()调用你刚刚创建的其余资源。用这个函数传递myData没有任何意义。数据()调用将返回Resource实例,该实例将具有接受参数的您的update api函数。

而且,在API构造时间传递数据没有意义。

Here是完整的参考文献

0

我认为这是因为“data”是一个返回$ resource对象的函数。 尝试下面的场景:

// service 
    .factory('api', function ($resource) { 
     var api = {}; 

     api.issues = $resource("http://localhost:4000/api/issues"); 

     api.users = $resource("http://localhost:4000/api/users", {}, { 
     update: { 
      method: 'PUT', 
      headers: { 
      'Content-Type': 'application/x-www-form-urlencoded' 
      } 
     }, 
     }); 

     return api; 
    }) 

    // controller 
    api.users 
     .update({where:{name:'kevin'}}) 
     .$promise.then(function(success) { 
     // DO SOMETHING 
     }); 

    ... 

    api.issues.query().$promise.then(
     function(success) { 
     // DO SOMETHING 
     }); 
相关问题