简体   繁体   中英

Access to method in the same .factory in Angular.js

I run the factory, and excecute the function "fn_generarPopupConfirm()" in any controller. in this method, I have now created a template. This template has a buton that has an ng-click, which calls an existing function inside the same factory. In my example I have this:

<button  type="submit" class="btn btn-primary" ng-click="fn_confirmar()">

How can I do it to call it ("oElim.fn_confirmar()")?, without needing to define a function in which I define a $scope object, to call the function needed. this function is present in the same factory.

controller: function($scope){

    $scope.fn_confirmar=function(){
        oElim.fn_confirmar();
    }
},

I need the function to be called directly "oElim.fn_confirmar()" with the ng-click event. it's possible?

this is my factory.

.factory('eliminar', function($state,$rootScope,$uibModal,popup_generico) {
  var oElim= {};

  oElim.fn_generarPopupConfirm = function(objeto,array,titulo,contenido) {

  $rootScope.modalInstances.push($uibModal.open({
    template: '<form id="form_popup" class="form-horizontal"><div class="modal-header">
   <h3 class="modal-title"><button  type="submit" class="btn btn-primary" 
    ng-click="fn_confirmar()">
    OK</button></div></form>',
    controller: function($scope){

        $scope.fn_confirmar=function(){
            oElim.fn_confirmar();
        }
    },
    backdrop: 'static'
  }));

}

 oElim.fn_confirmar = function(){
    var index =  oElim.array.indexOf(oElim.objeto);
    oElim.array.splice(index, 1);
    popup_generico.fn_CerrarModal();
 }
    return oElim;
})

I do not believe that this is possible within angular, as the template is a string that will be interpolated within the context of the controller, not the factory.

If you really want to have scope access to the oElim factory without injecting it into a controller, you could bind the oElim object directly to the $rootScope, giving you prototypal access to its methods within the template "$rootScope.oElim.fn_confirmar()" or equivalently just "oElim.fn_confirmar()" from any template you define in your angular app.

As I know it's not possible, but you can do the following:

 controller: function($scope){ $scope.fn_confirmar = oElim.fn_confirmar; }, 

or add the object to $rootScope

 .factory('eliminar', function($state,$rootScope,$uibModal,popup_generico) { var oElim= {}; $rootScope.oElim = oElim; oElim.fn_generarPopupConfirm = function(objeto,array,titulo,contenido) { $rootScope.modalInstances.push($uibModal.open({ template: '<form id="form_popup" class="form-horizontal"><div class="modal-header"> <h3 class="modal-title"><button type="submit" class="btn btn-primary" ng-click="oElim.fn_confirmar()"> OK</button></div></form>', controller: Function.prototype, //Just dummy function backdrop: 'static' })); } oElim.fn_confirmar = function(){ var index = oElim.array.indexOf(oElim.objeto); oElim.array.splice(index, 1); popup_generico.fn_CerrarModal(); } return oElim; }) 

or better, just used built-in angular event service

 .factory('eliminar', function($state,$rootScope,$uibModal,popup_generico) { var oElim= {}; oElim.fn_generarPopupConfirm = function(objeto,array,titulo,contenido) { $rootScope.modalInstances.push($uibModal.open({ template: '<form id="form_popup" class="form-horizontal"><div class="modal-header"> <h3 class="modal-title"><button type="submit" class="btn btn-primary" ng-click="$emit('fn_confirmar')"> OK</button></div></form>', controller: Function.prototype, //Just dummy function backdrop: 'static' })); } oElim.fn_confirmar = function(){ var index = oElim.array.indexOf(oElim.objeto); oElim.array.splice(index, 1); popup_generico.fn_CerrarModal(); } $rootScope.$on('fn_confirmar', oElim.fn_confirmar); return oElim; }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM