Use Angularjs to pass parameter to Jquery

Asked

Viewed 1,307 times

1

I need to change my web system’s Alerts template. For this I am using a Jquery plugin, however, I need to pass a parameter to my Jquery function that this, would be my model that is already used in Angularjs.

in this function by clicking the delete button, I cannot access the model, {{sector}}, after this, I still need to navigate to a link by clicking the 'Confirm' button'.

I’d like to do something like this:

<script>
        $('.alertaExcluir').click({ setorId: '{{setorId}}' }, function () {
            $.confirm({
                icon: 'fa fa-warning',
                title: 'Confirmação',
                keyboardEnabled: true,
                content: 'Você confirma a exclusão?',
                confirmButton: 'Sim',
                cancelButton: 'Não',
                animation: 'top',
                confirm: function () {
                    alert(setorId)
                },
                cancel: function () {
                    alert('Canceled!')
                }
            });
        })
    </script>

<div class="col-sm-1" style="text-align: center; padding-right: 5px; padding-left: 0px;">
     <a class="alertaExcluir {{classeBtnEdit}}" style="color: black">
          <img style="max-height: 50px;" src="../../Images/tecbox/icons/delete-icon.png" />
          <span style="padding-top:20px;">Excluir</span>
     </a>
</div>
  • 2

    Angularjs works well with jQuery. But if you want to put a little effort into it, you can do a modal template with Angularjs. Check here: https://docs.angularjs.org/guide/templates

  • Thanks for the tip, but I would like to see it working, I don’t understand why I can’t get {{setorId}} inside this Jquery, it’s more for 'pride' really, because it should work. But as for the Angular template, know some tutorial in Portuguese?

  • 2

    The purpose of Angularjs is to separate javascript from the view. In this case, you are making wrong use. The correct thing was to keep the whole javascript structure within the angular application. And call the method internally, passing only the controller’s Scope through the view. I’ll put an example as an answer here, ok.

  • how do I separate the {{foo}} angular tag from the view? I think this is not wrong, but now separate into a file and have it loaded is barbada in the question jquery, only that only do it after seeing working, it is more practical to work this way.

  • Dude, this isn’t Smarty, you can’t implement variables within javascript methods. Angular does an internal treatise for html.

  • You have to respect the working standards of Angularjs, otherwise you will not leave the place.

  • Take the course to the free part first to understand how it works and what the proposal of Angularjs is, otherwise you will suffer face: http://campus.codeschool.com/courses/shaping-up-with-angular-js/

  • You can’t solve the problem?

  • 1

    I can do the modal you want, but that’s not the way you’re doing it. I’ll put the answer here, for you to understand, but it will take a while.

  • What do you call your controller? and your application name?

  • Setorcontroller

  • @Ivanferrer awaiting your response.

Show 7 more comments

2 answers

1


Pass confirmFX into your controller, as in the example below:

angularApp.controller('SetorController', ['$scope', '$routeParams', '$location', '$filter', '$http', 'confirmFx',
    function ($scope,  $routeParams, $location, $filter, $http, confirmFx) {

$scope.deletarRegistro = function(data) {
 /* aqui você coloca a requisição do banco que 
    faz a exclusão e dá um echo com
    json_encode(['success'=>true,'message'=>'Mensagem excluída com sucesso!']);
*/
     $.post('/deletar-registro', {id:data.setorId}, function(rtn) {
          if (rtn.success) {
              alert(rtn.message);
          }
     });
}

$scope.excluir = function (data) {
            confirmFx('Exclusão de arquivo', 'Você confirma a exclusão?',
            function () {
                $scope.deletarRegistro(data);
            });
        };

}]);   

In the view you must pass the Scope of the element you want to delete ng-click="excluir(data)".
In a "popup_functions.js" file the part:

function zIndexPopup() {
    var zInd = parseInt($('.ui-front ').css('z-index')) + 1000;
    $('.ui-front ').css('z-index', zInd)
    //console.log(zInd);
}

function confirmFx(titulo, content, arrayBotoes) {

    $(window).scrollTop(0);
    $('body').scrollTop(0);
    $("<div id='alertFx2' title='" + titulo + "' style='text-align:left'>" +
            "<table style='border:none'>" +
            "<tr>" +
            "<td>" + content + "</td>" +
            "<tr>" +
            "</table>" +
            "</div>").dialog({
        modal: true,
        buttons: arrayBotoes,
        autoOpen: false,
        position: {my: "top+120", at: "center top", of: window},
        drag: function(event, ui) {
            $(window).scrollTop(0);
            $('body').scrollTop(0);
        },
        open: function() {
            var id = $(this).attr("id");
            if (id != "alertFx2") {
                $(this).dialog("destroy");
            }
            zIndexPopup();
        }
    }).dialog('open');
}

Now put the modal template script in your view:

<script type="text/ng-template" id="confirmFxModal.html">
        <div class="modal-header">
            <h3 class="modal-title">{{title}}</h3>
        </div>
        <div class="modal-body" style="font-weight: normal;">            
            <div ng-bind-html="message">{{message}}</div>
        <div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="ok()">Sim</button>
            <button class="btn btn-default" ng-click="cancel()">Não</button>
        </div>
</script>

Now create a standard file for angular services "servicesDefault.js":

angularApp.factory('confirmFx', ['$modal', function ($modal) {

        function confirmFxCtrl($scope, title, content, $modalInstance, fnCallBack) {
            $scope.title = title;
            $scope.message = content;

            $scope.ok = function () {
                setTimeout(fnCallBack, 10);
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }

        function openPopup(title, content, fnCallBack) {

            var modalInstance = $modal.open({
                templateUrl: 'confirmFxModal.html',
                controller: confirmFxCtrl,
                size: 'sm',
                resolve: {
                    title: function () {
                        return title;
                    },
                    content: function () {
                        return content;
                    },
                    fnCallBack: function () {
                        return fnCallBack;
                    }
                }
            });
            return modalInstance;
        }

        return function (title, content, fnCallBack) {
            return openPopup(title, content, fnCallBack);
        };

    }]);

On your view, place at the end of the body:

<script src="//code.jquery.com/jquery-latest.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript" src="js/SetorController.js"></script>
<script type="text/javascript" src="js/popup_functions.js"></script>
<script type="text/javascript" src="js/servicesDefault.js"></script>

1

You can capture the variable you need using a type code:

angular.element('[ng-controller="SetorController"]').scope().setorId

  • you know how to navigate to another page using ajax? If yes, please put the code inside the same Jquery function that I have above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.