简体   繁体   English

NAN在Angular JS中分配给ng-repeat $ index

[英]NAN assigned to the ng-repeat $index in Angular JS

I have a hidden attribute in my HTML as 我的HTML中有一个隐藏属性

<input id="temp" type="hidden" name="country" value="{{$index + 1}}" ;>

Note that the {{$index + 1}} is the index for the ng-repeat written above the hidden tag. 请注意,{{$ index + 1}}是隐藏标签上方写入的ng-repeat的索引。

Now I am trying to get the value of this temp hidden variable(trying to get the value of the index). 现在,我试图获取此临时隐藏变量的值(试图获取索引的值)。

<script type="text/javascript" >
var tempo = document.getElementById("temp").value;
var tempi = parseInt(tempo) % 6;
console.log(tempi);
var imagepath = "img/products/coffee/coffee-"+tempi+".jpg" ;
document.getElementById("product-images").src = imagepath;
</script>

However, when I see the value assigned to the src attribute of my img tag, it is always NAN. 但是,当我看到分配给img标签的src属性的值时,它始终是NAN。

Note that the datatype of {{$index + 1}} is STRING. 请注意, {{$index + 1}}的数据类型为STRING。 So , I am converting it to integer and trying to do the mod operation. 因此,我将其转换为整数并尝试执行mod操作。 Still getting NAN for the tempi variable. 仍然为tempi变量获取NAN。 Any idea where I am going wrong ? 知道我要去哪里错了吗? Thank you so much. 非常感谢。

You can try this: 您可以尝试以下方法:

HTML: HTML:

<input id="temp" name="country" ng-init="hiddenValue = ($index + 1)" ng-model='hiddenValue'>

JS: JS:

<script type="text/javascript" >
    function MyCtrl($scope) {
        $scope.yourAngularFunction = funcion() {
            console.log($scope.hiddenValue);
        }
    }
</script>

Although there are actually better ways to work the general logic 尽管实际上有更好的方法可以处理一般逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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