Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays angularjs angular不';t读取数组的长度_Arrays_Angularjs_Radio Button - Fatal编程技术网

Arrays angularjs angular不';t读取数组的长度

Arrays angularjs angular不';t读取数组的长度,arrays,angularjs,radio-button,Arrays,Angularjs,Radio Button,在过去的几天里,我一直在为安格拉斯的问题而挣扎。我是这方面的新手,这就是我的麻烦产生的原因。 无论如何,这是我的问题。我有一个应用程序,用于向用户提问、收集答案并向用户显示。 HTML是: <div ng-repeat="dialog in dialogWindows"> <div id="{{dialog.idName}}" class="bold abs"> <div class


在过去的几天里,我一直在为安格拉斯的问题而挣扎。我是这方面的新手,这就是我的麻烦产生的原因。
无论如何,这是我的问题。我有一个应用程序,用于向用户提问、收集答案并向用户显示。
HTML是:

   <div ng-repeat="dialog in dialogWindows">
                <div id="{{dialog.idName}}" class="bold abs">   
                    <div class="questionContainer rel">


         <a href=""><button ng-click="compute()>Fake results</button></a>

                        <div ng-repeat="input in dialog.inputs">
                        <input type="radio" id="{{input.radio}}" name="{{dialog.name}}" value="{{input.value}}">
                        <label for="{{input.radio}}" class="answer abs {{input.a}}">{{input.answer}}</label>


                        </div>
                        </div>



                </div>
                </div><!--/ng-repeat-->
             </div><!--/ng-controller-->
没有什么真正复杂的,到目前为止,它工作得很好。现在您可以看到ng repeat生成了三个单选按钮。我们给按钮分配了计算功能,很快你就会看到它的功能。 下面是compute()函数:

$scope.compute = function() {



    if (document.getElementById('radio1').checked) {

        $scope.a.push(1);
        $scope.b.push(1);
        $scope.c.push(1);
        $scope.d.push(1);
        $scope.e.push(1);
        $scope.f.push(1);
        $scope.g.push(1);
        $scope.h.push(1);
        $scope.i.push(1);
        $scope.j.push(1);
        $scope.k.push(1);
        $scope.l.push(1);
        $scope.m.push(1);
        $scope.n.push(1);
        $scope.o.push(1);
        $scope.p.push(1);

    } else if (document.getElementById('radio2').checked) {

       $scope.r.push(1);
       $scope.s.push(1);
       $scope.t.push(1);
       $scope.u.push(1);
       $scope.w.push(1);


    } else if(document.getElementById("radio3").checked){
        $scope.z.push(1);
        $scope.x.push(1);
        $scope.y.push(1);
        $scope.q.push(1);
        $scope.ab.push(1);

        }
回答的问题将传递给负责收集答案的第12个阵列之一。 JS:

然后我写了一个元素列表,每个元素代表一个数组,也就是说

<div ng-repeat="record in records">
<a href="{{record.link()}}"><div class="rel fptsans {{record.className()}}">{{record.item}}</div></a>
</div>
我确信应用程序的每个部分都是一致的,但很快我就会惊讶地发现Angular在ng repeat=“record in records”中没有显示任何结果,因为它引用的是一个空对象($scope.a=[];在初始化时实际上是空的),尽管我可以通过简单地在html{{a.length}中写入来查看数组的长度,但显然数组的长度在增加。
我的问题是如何在角度数组中使用$scope.[some array].length。我应该使用带有单选按钮的ng型号吗?会有帮助吗?我该如何解决这个让我陷入困境的问题。请帮助我解决问题。提前感谢您

我想您正在调用计算之前填充$scope.records。这将导致开始时数组为空。您需要通过观察数组来重新填充$scope.records。看看$watch,看看它是如何工作的。我个人认为你不应该把所有的a、b、c等项目都放在$scope中,但是你应该把它们放在$scope.Questions.a、$scope.Questions.b、$scope.Questions.c等中。然后你可以在$scope.Questions或单个项目上创建一个手表

[编辑] 我知道问题出在哪里了!在我开始解释这种情况之前,我想建议您阅读更多关于AngularJS的内容,以及如何在AngularJS的世界中思考(也许这会有所帮助)

代码有几个问题:首先,类定义中没有{{

<div class="record.className}}">
这不是解决问题的最佳方法,但却是最容易与您沟通的方法。其他方法是在alpeic数组上设置$watch,然后重新评估$scope.Records中的属性。此外,您可以也应该使用ng类设置类属性,而无需范围:

<div ng-repeat="record in records">
    <a href="alpeic.html">
        <div ng-class="{special: alpeic.length >= 5, normal: alpeic.length < 5}">{{record.item}}</div>
   </a>                          
</div>


建议在jsfiddle.net或plunker中创建一个简单的演示程序,显示您的问题并询问您的答案。我尝试在$scope.a=[]上使用$watch;如下所示:$scope.$watch('a',compute,true);但控制台返回错误$watch undefined。您从哪里获取$scope?它是angular注入的依赖项吗?我不确定我是否理解您的意思。我指的是$scope。$watch放在dialogWindows控制器中。我使用$watch的标准语法,如“AngularJS”中所述作者:布拉德·格林和希亚姆·塞沙德里。我想我会准备一把小提琴来说明大多数神经痛的问题。正如我在上一篇文章中所说,这里是展示这个问题的提琴-
$scope.records = [
    {id:0, 
    className :  $scope.a.length > 0 ? 'special' : 'normal',    
    item: "a",
    link: $scope.className == "special" ? "a.html" : ''

    },
    {id:1,
    className: $scope.b.length > 0 ? 'special' : 'normal',
    item:"b",
    link: $scope.className == "special" ? "b.html" : ''

    },
    {id:2,
    className:  $scope.c.length > 0 ? 'special' : 'normal',
    item:"c",
    link: $scope.className == "special" ? "c.html" : ''
    },
//and so on to 12th.
<div class="record.className}}">
<div class="{{record.className}}">
$scope.compute=function()
{
    $scope.alpeic.push(1);
    $scope.records = [
    {id:0,
     link:$scope.alpeic.length > 5 ? "alpeic.html" : "",
     className:$scope.alpeic.length > 5 ? "special" : "normal",
     item:"This is item A"},
    {id:1,
    link:$scope.alpeic.length > 5 ? "alpeic.html" : "",
    className:$scope.alpeic.length > 5 ? "special" : "normal",
     item:"This is item B"}];
}

$scope.compute();
<div ng-repeat="record in records">
    <a href="alpeic.html">
        <div ng-class="{special: alpeic.length >= 5, normal: alpeic.length < 5}">{{record.item}}</div>
   </a>                          
</div>