2017-05-07 78 views
1

我想從角度控制器發送$範圍到一個JavaScript函數,但在調用我的JavaScript函數(teste2)$範圍總是未定義。

我還需要傳遞的$ HTTP,咆哮和ngProgressFactory到我的javascript函數...

angular.module('geonosis').controller('ListagemPessoasController', function (DTOptionsBuilder, DTColumnBuilder, $http, $q, $scope, growl, ngProgressFactory, $window) { 

     $scope.progressbar = ngProgressFactory.createInstance(); 

     var vm = this; 
     vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() { 
      $scope.progressbar.start(); 
      var defer = $q.defer(); 
      $http({ 
       method: 'GET', 
       url: 'http://localhost:8082/pessoa/3', 
       headers: { 
        'Authorization': '328316ed-41dd-491d-b362-d80ec55d5ebd' 
       } 
      }).then(function successCallback(response) { 
       defer.resolve(response.data); 
       $scope.progressbar.complete(); 
      }, function errorCallback(response) { 
       $scope.progressbar.complete(); 
       growl.error("<b>Erro ao consultar pessoas</b>", {}); 
      }); 
      return defer.promise; 
     }) 
     .withLanguage({ 
      sUrl: '//cdn.datatables.net/plug-ins/1.10.15/i18n/Portuguese-Brasil.json' 
     }); 

     vm.dtColumns = [ 
      DTColumnBuilder.newColumn('nome').withTitle('Nome').withOption('width', '40%').withClass('text-center'), 
      DTColumnBuilder.newColumn('categoriaPessoa').withTitle('Categoria').withOption('width', '25%').withClass('text-center'), 
      DTColumnBuilder.newColumn('cidade').withTitle('Cidade').withOption('defaultContent', 'não informada').withOption('width', '25%').withClass('text-center'), 
      DTColumnBuilder.newColumn(null).withTitle('').withOption('width', '10%').withClass('text-center').notSortable() 
       .renderWith(function(data, type, full, meta) { 
        return '<a class="btn btn-primary btn-sm" onclick="teste2(' + data.idPessoa + ')">' + 
          ' <i class="fa fa-trash-o"></i></a>' 
          + 
          '<button class="btn btn-primary btn-sm" onclick="edit2(' + data.idPessoa + ')">' + 
          ' <i class="fa fa-pencil-square-o"></i></button>'; 
       }) 
     ]; 
}); 

function teste2(id, $scope){ 
    console.log(scope); //ALWAYS UNDEFINED!! 
} 

如果我把睾丸中的角escope內,我收到:沒有定義 「teste2」」

+0

你需要在'Controller'創建'teste2'方法來訪問'$ scope' –

+0

是阿甘,這就是問題所在。但我改變了我的所有代碼,以遵循本教程:http://l-lin.github.io/angular-datatables/archives/#!/angularWayDataChange –

+0

嘗試控制器中的'$ scope.teste2 = teste2' –

回答

1

$範圍是棱角分明,爲了獲取你必須使用你的應用程序模塊控制器函數內部的$範圍提供了一個JavaScript對象。編輯並移動合作ntroller功能:

angular.module('geonosis').controller('ListagemPessoasController', yourCtrlFunction($scope,...other_injections){ 
function teste2(id, $scope){ 
    console.log($scope); 
}}); 
+0

感謝您的答案hamzox,我解決了這個問題鏈接:http://l-lin.github.io/angular-datatables/archives/#!/angularWayDataChange –

+0

太棒了!至少Upvote它。 – hamzox

1
function teste2(id, $scope){ 
    console.log(scope); //missing $ console.log($scope) 
}