0
我有一個基本的工廠返回稱爲用戶$資源:
angular.module('appServices').factory('User', [
'$resource',
function($resource){
return $resource('http://localhost:3000/users/:username', {username:'@username'});
}]);
在我UserController.js文件我嘗試做一些非常簡單:
angular.module("myApp").controller('UserController', ['$scope', '$routeParams', 'User',
function($scope, $routeParams, User){
var user = new User({email:'[email protected]', username: 'johndoe', password: 'mySecurePassw0rd!'});
user.$save();
}]);
我希望$save()
功能,使POST請求/users
,但在控制檯中我得到http://localhost:3000/users/johndoe
404響應,因爲路線顯然是不成立的服務器與附加username參數POST請求在...
爲什麼它在我的代碼示例中這樣做?從我在互聯網上看到的例子中,$save()
函數沒有考慮用戶名,並且應該直接POST到/users
對我來說。
任何幫助,將不勝感激!
編輯
我想我得到了錯誤(一如既往,張貼這樣的問題......之後)
這可能是因爲我在$資源指定的默認參數{username: "@username"}
的,因爲它預計默認情況下有一個用戶名參數?
所以正確的方式做這將是從工廠返回此資源:
return $resource('http://localhost:3000/users/:username');
是否正確?