繁体   English   中英

角从ng-repeat中的$ scope获取动态变量

[英]Angular get dynamic variable from $scope inside ng-repeat

我无法在ng-repeat的$ scope中输出变量

我的$ scope中定义了以下变量: error_question1error_question2error_question3

我在ng-repeat中包含以下内容,并尝试使用ng-repeat的$ index将范围的值分配给$ scope中的相应变量。

我可以通过直接定位变量来使它工作,但是很明显,它对我的​​ng-repeat中的每个项目都重复如下:

<span class="help-block" ng-show="error_question{{$index + 1}}">{{error_question2}}</span>

但是,我认为它无法正常工作:

<span class="help-block" ng-show="error_question{{$index + 1}}">{{error_question[$index + 1]}}</span>

[$ index + 1]似乎被破坏了吗?

请注意,通过执行{{error_question[$index + 1]}}您基本上是在说“让我在error_question数组的$index + 1位置获得项目”。 那不是你想要的。

尝试使用其他方法。 而不是在$scope浮动所有的error_question1,error_question2,... error_questionX ,请使用一个对象来封装错误。 像这样:

$scope.errors = {
  question1: ...,
  question2: ...,
  question3: ...,
  ...
};

这样,您可以像下面这样在HTML中使用它:

<span class="help-block" ng-show="errors.question{{$index + 1}}">{{errors['question' + ($index + 1)]}}</span>

柱塞

暂无
暂无

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

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