如何将变量从jQuery传递给AngularJs函数

时间:2017-10-26 07:25:05

标签: javascript jquery angularjs arrays

我的控制器上有这个功能

当我点击image.svg的一部分时,我使用此函数将svg路径的id保存在一个数组上

myBtn

我想在同一个Controller上访问数组:arrayCuerpo on this function:

$(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;
            }
        });

    });

编辑解决方案:

    $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);

1 个答案:

答案 0 :(得分:-1)

你可以这样做:

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