【发布时间】:2018-04-07 11:19:02
【问题描述】:
我的控制器上有这个功能
当我单击 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;
}
});
});
我想访问数组: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();
【问题讨论】:
-
我想建议在元素上使用 ng-click 而不是在 jquery 上使用 on 'click' 来触发 'clicked' 事件。 您可以使用 jquery 在 SendForm1 函数范围代码上进行厄运操作。
-
我忘了说我在 svg 路径上使用那个类 (SaveBody) .. (我明白了。因此,您可以使用的一种选择是使用全局 JavaScript 变量(不推荐这样做)。但是如果您想将 svg 标签与 angularjs ng-click 集成,可能此链接可以帮助您stackoverflow.com/a/29868537/5129606ng-click 不起作用 :( ,当我单击 img 的一部分时,它什么也不做,在该函数内没有警报或 console.log是我的错,在 svg-image 上没有调用 ng-controller!
标签: javascript jquery angularjs arrays