指令未正确更新控制器变量

时间:2016-06-10 23:46:11

标签: angularjs angularjs-directive angularjs-scope

我对Angular很新,我正在尝试从指令中更新我的一个控制器变量,但它似乎不起作用。该变量填充在指令中,当我在那里记录它时没关系。然后当我尝试从控制器记录变量时;它首先记录正常,然后是“未定义”。

我不知道为什么会这样。帮助:(

指令

app.directive('reportFilter', function() { 
return { 
 restrict: 'E', 
 scope: {}, 
controller: 'MainController', 
templateUrl: 'js/directives/reportFilter.html',
link: function(scope, element, attrs) {

scope.updateTable = function(selectedOffer,startDate,endDate) {
var offer='offer:'+selectedOffer.offer_id;
        $.ajax({
            type: 'POST',
            url: 'daily_summary.php',
            data: { 
                'offer': selectedOffer.offer_id 
        },
        dataType: 'json',
        cache: false,
        success: function(response) {
          scope.myvariable=response;
          scope.getData();
          console.log(scope.myvariable);
          scope.$apply();
          }
    });
    }
  }
}

});

控制器

  app.controller('MainController', ['$scope', 'service',   'Offerservice','$attrs', function($scope, service,Offerservice) {

  $scope.getData= function () {
          console.log($scope.myvariable);
          return $scope.myvariable;
      };
 }]);

This is the console's log showing the variable become undefined again in the controller

0 个答案:

没有答案