$resource 未更改 url 的 ID
$resource is not changing the id of the url
我很难更改传递给 $resource
的 URL 的 id 参数。显然,即使我输入固定值 (Resource.get(id:1))
,该值也没有更改为它从 Resource.get(id:$routeParams.id)
接收到的正确值,导致以下错误:
TypeError: encodeUriSegment is not a function
当我将 URL 的 id 参数更改为固定值 (baseURL+'client/1')
时,它起作用了。
这是我的字体:
app.js
'use strict';
angular.module('serviceOrder',['ngRoute','ngResource'])
.config(function ($routeProvider,$locationProvider) {
/*$locationProvider.html5Mode({
enabled: true,
requireBase: false
});*/
$routeProvider.when('/clients', {
templateUrl: 'views/clients.html',
controller: 'ClientController'
});
$routeProvider.when('/newclient',{
templateUrl: 'views/client.html',
controller: 'NewClientController'
});
$routeProvider.when('/editclient/:id',{
templateUrl: 'views/client.html',
controller: 'EditClientController'
});
$routeProvider.otherwise({redirectTo: '/clients'});
});
controler.js
'use strict';
angular.module('serviceOrder')
.controller('EditClientController',['$scope','$routeParams','clientService',
function ($scope,$routeParams,clientService) {
$scope.message = 'Loading ...';
$scope.client = {};
$scope.phone = {id:'',brand:'',model:'',state:'',esn:''};
debugger;
clientService.getClients().get({id:$routeParams.id})
.$promise.then(
function (response) {
$scope.client = response;
},function (error) {
$scope.message = 'Error: ' + error;
}
);
}]);
service.js
'use strict';
angular.module('serviceOrder')
.constant('baseURL', 'http://localhost:8080/service-order-rest/rest/')
.service('clientService',['$resource','baseURL',function ($resource,baseURL){
this.getClients = function () {
return $resource(baseURL+'client/:id',null,{'update':{method:'PUT'}});
};
}]);
我们这里有一个错误:
return $resource(baseURL+'client/:id',{id: youridhere} ,{'update':{method:'PUT'}});
您的参数默认值配置不正确。为此,根据 $resource
's docs,您必须指定 API 方法的模式,该模式将接收 { id: '@id' }
之类的参数,而不是 null
.
$resource(baseURL + 'client/:id', //url
{ id: '@id' }, // parameters
{
'update': { method: 'PUT' } // methods
});
我很难更改传递给 $resource
的 URL 的 id 参数。显然,即使我输入固定值 (Resource.get(id:1))
,该值也没有更改为它从 Resource.get(id:$routeParams.id)
接收到的正确值,导致以下错误:
TypeError: encodeUriSegment is not a function
当我将 URL 的 id 参数更改为固定值 (baseURL+'client/1')
时,它起作用了。
这是我的字体:
app.js 'use strict';
angular.module('serviceOrder',['ngRoute','ngResource'])
.config(function ($routeProvider,$locationProvider) {
/*$locationProvider.html5Mode({
enabled: true,
requireBase: false
});*/
$routeProvider.when('/clients', {
templateUrl: 'views/clients.html',
controller: 'ClientController'
});
$routeProvider.when('/newclient',{
templateUrl: 'views/client.html',
controller: 'NewClientController'
});
$routeProvider.when('/editclient/:id',{
templateUrl: 'views/client.html',
controller: 'EditClientController'
});
$routeProvider.otherwise({redirectTo: '/clients'});
});
controler.js
'use strict';
angular.module('serviceOrder')
.controller('EditClientController',['$scope','$routeParams','clientService',
function ($scope,$routeParams,clientService) {
$scope.message = 'Loading ...';
$scope.client = {};
$scope.phone = {id:'',brand:'',model:'',state:'',esn:''};
debugger;
clientService.getClients().get({id:$routeParams.id})
.$promise.then(
function (response) {
$scope.client = response;
},function (error) {
$scope.message = 'Error: ' + error;
}
);
}]);
service.js 'use strict';
angular.module('serviceOrder')
.constant('baseURL', 'http://localhost:8080/service-order-rest/rest/')
.service('clientService',['$resource','baseURL',function ($resource,baseURL){
this.getClients = function () {
return $resource(baseURL+'client/:id',null,{'update':{method:'PUT'}});
};
}]);
我们这里有一个错误:
return $resource(baseURL+'client/:id',{id: youridhere} ,{'update':{method:'PUT'}});
您的参数默认值配置不正确。为此,根据 $resource
's docs,您必须指定 API 方法的模式,该模式将接收 { id: '@id' }
之类的参数,而不是 null
.
$resource(baseURL + 'client/:id', //url
{ id: '@id' }, // parameters
{
'update': { method: 'PUT' } // methods
});