簡體   English   中英

從AngularJS中的回調函數內部訪問控制器

[英]Accessing a controller from inside a callback function in AngularJS

我有以下AngularJS控制器:

controllers.controller('QueuesCtrl', ['$scope', 'QueueRes',function($scope,QueueRes) {

    $scope.queues = QueueRes.query();
    this.queue={};

    this.create = function() {
        QueueRes.save(this.queue,function(){
            this.queue={};
        })

    };

  }]);

對象this.queue是一種表單,我想在成功發布數據后將其重置。 this.queue={}; 回調函數內部不起作用(這很有意義,因為在該上下文中this是不同的)。 如果我移動this.queue={}; 到回調之外,我的代碼可以工作,但是無論POST操作的結果如何,都會重置密碼,這不是我想要的。

如何從回調內部訪問控制器?

this (控制器對象參考)與$ scope (綁定到html模板的視圖模型)不同。

如果要在$ scope上重置queue ,可以直接使用

$scope.queue = {};

否則,你可以存儲this一個變量並使用該變量來設置隊列。

controllers.controller('QueuesCtrl', ['$scope', 'QueueRes',function($scope,QueueRes) {
    var me = this;

    $scope.queues = QueueRes.query();
    me.queue={};
    this.create = function() {
        QueueRes.save(this.queue,function(){
            me.queue={};
        })
    };
  }]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM