【发布时间】:2015-01-22 22:56:31
【问题描述】:
我是 Angular 的新手,我一直在尝试一些有趣的东西来结识。我似乎无法得到工作的承诺。更新操作工作正常。我没有在控制台中打印任何内容。
app.factory "Profile", [
"$resource"
"$q"
($resource, $q) ->
Profile = ->
@service = $resource("/users/profiles/:id.json", {id: @id}, 'update': {method: 'PATCH', params: {id: '@id'}, headers: {'Content-Type': 'application/json'}})
return
Profile::show = (userId) ->
@service.get(id: userId)
Profile::update = (updatedProfileObject) ->
deferred = $q.defer()
@service.update(id: updatedProfileObject.id, profile: updatedProfileObject).success = (response) ->
deferred.resolve
message: "great success borat"
return
deferred.promise
return new Profile
]
app.controller "MainCtrl", [
"$scope"
"$routeParams"
"Profile"
"$route"
"$rootScope"
"$q"
($scope,$routeParams,Profile,$route,$rootScope,$q) ->
$rootScope.$on "$routeChangeSuccess", ->
$scope.profile = Profile.show($routeParams.id)
return
$scope.range = (min, max, step) ->
step = (if (step is `undefined`) then 1 else step)
input = []
i = min
while i <= max
input.push i
i += step
input
$scope.update = ->
promise = Profile.update($scope.profile)
promise.then = (response) ->
console.log(response.message)
]
更新了代码以修复控制器更新方法和工厂更新方法的语法,它现在仍然有效。
更新代码:
app.factory "Profile", [
"$resource"
"$q"
($resource, $q) ->
Profile = ->
@service = $resource("/users/profiles/:id.json", {id: @id}, 'update': {method: 'PATCH', params: {id: '@id'}, headers: {'Content-Type': 'application/json'}})
return
Profile::show = (userId) ->
@service.get(id: userId)
Profile::update = (updatedProfileObject) ->
deferred = $q.defer()
@service.update(id: updatedProfileObject.id, profile: updatedProfileObject).$promise.then (value) ->
deferred.resolve();
deferred.promise
return new Profile
]
app.controller "MainCtrl", [
"$scope"
"$routeParams"
"Profile"
"$route"
"$rootScope"
"$q"
($scope,$routeParams,Profile,$route,$rootScope,$q) ->
$rootScope.$on "$routeChangeSuccess", ->
$scope.profile = Profile.show($routeParams.id)
return
$scope.range = (min, max, step) ->
step = (if (step is `undefined`) then 1 else step)
input = []
i = min
while i <= max
input.push i
i += step
input
$scope.update = ->
Profile.update($scope.profile).then (value) ->
console.log("lol")
【问题讨论】:
-
defer.resolve接受一个应该是解析数据的参数。 -
我将数据放入解决方案中,但仍然遇到同样的问题
-
终于成功了吗?
标签: angularjs coffeescript promise