如何调用$ http body angularjs里面的方法

时间:2016-12-22 07:33:44

标签: javascript angularjs angularjs-directive angularjs-scope

我想在我的其他方法中调用editopenComponentModal显示错误angular.js:13920 TypeError: Cannot read property 'editopenComponentModal' of undefined

   EditCurrentJob(job) {

          this.$http.put(properties.job_path + "/"+job.job_id).then(function successCallback(response) {
            console.log(response.data);
            this.current_job =  response.data;
            this.editopenComponentModal();

         }, 
          function errorCallback(response) {

          });
}

     editopenComponentModal() {
        var modalInstance = this.$uibModal.open({
          animation: this.animationsEnabled,
          template: require('./Report/editsubmittedinformation.html'),
          scope: this.$scope,
          size: 'lg'
        });
        this.$scope.modalInstance = modalInstance;

        return modalInstance.result;
      }

3 个答案:

答案 0 :(得分:1)

为此目的使用函数引用,jopefully这将帮助你。

var vm = this;
vm.editopenComponentModal = editopenComponentModal;
function EditCurrentJob(job) {

      $http.put(properties.job_path + "/"+job.job_id).then(function successCallback(response) {
        console.log(response.data);
        vm.current_job =  response.data;
        vm.editopenComponentModal();

     }, 
      function errorCallback(response) {

      });
}

 function editopenComponentModal() {
    var modalInstance = this.$uibModal.open({
      animation: this.animationsEnabled,
      template: require('./Report/editsubmittedinformation.html'),
      scope: this.$scope,
      size: 'lg'
    });
    this.$scope.modalInstance = modalInstance;

    return modalInstance.result;
  }

答案 1 :(得分:0)

如果你想在$ http.put请求后打开一个模态,那么使用。

  $('#success').modal();

这里的成功就是身份。

答案 2 :(得分:0)

var that = this

上方添加this.$http.put(

然后改变:

this.current_job = response.data;

this.editopenComponentModal();

要:

that.current_job = response.data;

that.editopenComponentModal();

说明: 回调中的this具有不同的上下文,因此您需要将所需的this保存到可在此处使用的变量。

在这里你可以阅读更好的解释: How to access the correct `this` context inside a callback?