在angular $ promise中使用全局变量或函数

时间:2016-09-26 11:05:49

标签: javascript angularjs scope ecmascript-6 angular-promise

我在承诺中调用函数时遇到一些困难,它肯定与函数的范围有关。当promise执行并被解析时,会出现错误,指出函数未定义。

总结此代码: ok ()函数在收到服务器响应后关闭模式。 UserDetailsModel 是我在 UserdetailsController 中注入的服务。

所以有可能知道这个特定情况下有什么样的解决方案,因为我只想在承诺解决后关闭一个模态,但我不是非常精通角度和ES6?

提前感谢任何建议。

class UserDetailsController {

   constructor(UserDetailsModel){
      this.UserDetailsModel = UserDetailsModel;
   }; 

   //the ok function close a modal
   ok = function () {
      this.close();
   };

   //creates a json object with the POST method with angular $resource 
   this.UserDetailsModel.sendMessage().save({name :this.request.fullName,                    
                                             email:this.request.email})

   .$promise.then(function(response, status){

       /*****here is undefined*****/
       this.ok();
   },

   function(response) {
       message = "Error: "+response.status + " " + response.statusText;    
   });
}

0 个答案:

没有答案