Henrique Schmitt
Henrique Schmitt

Reputation: 195

Angular send $scope to javascript function

i'm trying to send $scope from a angular controller to a javascript function, but after call my javascript function (teste2) the $scope is always undefined.

I also need to pass $http, growl and ngProgressFactory to my javascript function...

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!!
}

if i put teste inside the angular escope, i receive: "teste2" is not defined"

Upvotes: 1

Views: 153

Answers (2)

haMzox
haMzox

Reputation: 2109

$scope is a javascript object provided by angular, in order to gain access to the $scope you must use it inside your app module controller function. Edit and move it to controller function:

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

Upvotes: 1

JS-Hero
JS-Hero

Reputation: 317

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

Upvotes: 1

Related Questions