1

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"

5
  • you need to create teste2 method in Controller to access the $scope Commented May 7, 2017 at 20:44
  • yes Agam, that was the problem. But i changed all my code to follow this tutorial: l-lin.github.io/angular-datatables/archives/#!/… Commented May 9, 2017 at 11:15
  • try $scope.teste2 = teste2 in controller Commented May 9, 2017 at 11:34
  • Aslo you need to change to console.log($scope) & then check Commented May 9, 2017 at 11:35
  • solved using l-lin.github.io/angular-datatables/archives/#!/… Commented May 9, 2017 at 19:22

2 Answers 2

1

$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); 
}});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer hamzox, i solved using this link: l-lin.github.io/angular-datatables/archives/#!/…
1
function teste2(id, $scope){
    console.log(scope); //missing $ console.log($scope)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.