Angular将$ scope发送到javascript函数

时间:2017-05-07 20:31:24

标签: angularjs

我试图将$ scope从角度控制器发送到javascript函数,但在调用我的javascript函数(teste2)后,$ scope始终未定义。

我还需要将$ http,growl和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!!
}

如果我把teste放在角度范围内,我会收到:&#34; teste2&#34;未定义&#34;

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

$ scope是由angular,提供的javascript对象,以便访问$ scope,您必须在app模块控制器函数中使用它。编辑并将其移动到控制器功能:

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