【问题标题】:Promise success not working承诺成功不起作用
【发布时间】: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


【解决方案1】:

去掉最后一行前面的“=”号,然后进行回调

promise.then (response) ->
                console.log(response.message)

【讨论】:

  • 感谢您再次回复。我试过了,现在它说成功不是一个功能:(
  • ... mmh $resource 不返回承诺。我想你需要使用 $promise (@service.update(..).$promise.then -> ...) 来访问它
  • 看起来您在控制器定义中缺少结束 ]
猜你喜欢
  • 2017-04-29
  • 2016-06-07
  • 2016-06-16
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多