簡體   English   中英

如何將變量從jQuery傳遞到AngularJs函數

[英]How to pass a variable from jQuery to AngularJs function

我的控制器上有此功能

當我單擊image.svg的一部分時,我使用此功能將svg路徑的ID保存在一個數組上。

$(document).ready(function(){
        var arrayCuerpo=[];
        $('.SaveBody').on("click", function() {

            var idCuerpo=jQuery(this).attr("id");
            var parteCuerpo=CambiarCuerpo(idCuerpo);

            if( jQuery.inArray(parteCuerpo,arrayCuerpo) == -1){//No existe
                $('path#'+idCuerpo).css({ fill: "#4C9141",stroke: "#4C9141" });
                arrayCuerpo.push(parteCuerpo);
                //console.log("if");
                console.log(arrayCuerpo);
            }else{//existe
                $('path#'+idCuerpo).css({ fill: "#9e9e9e",stroke: "#9e9e9e" });
                arrayCuerpo.removeItem(parteCuerpo);

               // console.log("else");
                console.log(arrayCuerpo);
                $scope.arrayCuerpo =arrayCuerpo;
            }
        });

    });

我想訪問同一控制器上此函數的array:arrayCuerpo:

    $scope.SendForm1 = function(){

        console.log($scope.arrayCuerpo);
         //This return undefined
    }

SendForm1 is called when I put my Send Button Form

Could u help me to pass that array in that Function pls? 

編輯解決方案:

I wasnt calling the ng-controlelr on the svg-image
For pass the variable i just created a service .factory

app.factory('yourService', function () {
    var array = [];

    return {
        setData: function setData(data) {
            array = data;
        },

        getData: function getData() {
            return array;
        }
    };
});

這樣我可以使用以下函數調用服務:

yourService.setData($scope.arrayCuerpo);

然后我得到的數據:

$scope.Array = yourService.getData();

您可以這樣:

$scope.SendForm1 = function(arrayCuerpo){
$scope.arrayCuerpo=arrayCuerpo;
    console.log(arrayCuerpo);
     //This return undefined
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM