使用AngularJS

时间:2016-04-27 22:44:31

标签: arrays angularjs json onsen-ui monaca

我正在尝试使用AngularJS在视图中显示JSON嵌套数组中的值。

我已经检查了console.log()上的输出,并按预期返回了JSON对象。但我想在我的视图中显示细节。特别是我想将它们显示为具有相关子级值的所有高级值。

这是我的app.js获取JSON并对其进行迭代以获取所有高级值及其关联的子级别项。

angular.forEach($scope.data, function(value, key)
{
    // Gets the high level items - WORKING
    console.log("Check Item Description: " + value.checkitemdesc);

    angular.forEach(value.options, function(v, k)
    {
        // Gets the sub-level items - WORKING
        console.log("Check ID: " + v.fleetcheckid);
        console.log("Check Value Description: " + v.checkvaluedesc);
    });
});

我想在温泉用户界面Switch in List Item Component

中显示高级项目
<ul class="list">
    <li class="list__item" ng-repeat="val in value">  
    {{val.checkitemdesc}} <!-- NOT WORING HERE -->
        <label class="switch switch--list-item">
            <input type="checkbox" 
                class="switch__input" 
                checked 
                onclick="modal.show('modal')">
            <div class="switch__toggle"></div>
        </label>
    </li>
</ul>

如何访问forEach循环返回的值数组?我试图在我的外部forEach循环中创建一个数组,然后将每个值推送到它但是得到错误,说值 undefined

$scope.itemDescriptionArray.push(value.checkitemdesc);

1 个答案:

答案 0 :(得分:2)

假设您有这种层次结构:

 private void ThreadProc(object obj)
    {
        String data = String.Empty;
        {
            using (var client = (TcpClient)obj)
            {
                using (var stream = client.GetStream())
                {
                    byte[] data2 = new byte[client.ReceiveBufferSize];

                    StringBuilder fullReply = new StringBuilder();

                    int numberOfBytesRead = 2;
                    stream.ReadTimeout = 1000;

                    try {
                        do
                        {
                            if (numberOfBytesRead <= 1) continue;

                            numberOfBytesRead = stream.Read(data2, 0, data2.Length);


                            fullReply.AppendFormat("{0}", Encoding.ASCII.GetString(data2, 0, numberOfBytesRead));

                            if (fullReply.Length == 0) continue;

                            log.Info("Odebrano wiadomość");
                        } while (stream.DataAvailable);
                    }

                    catch(Exception)
                    { Console.WriteLine(" EXCEPTION ON read  !!!"); };

                    var mesagesList = new List<Message>();

                    {

                        var messageString = fullReply.ToString();

                          var messageType = MessageManager.GetResponse(messageString);

                        dynamic T = this;
                        if (messageType != null)
                        {
                            Console.WriteLine("ODEBRANO: ", messageType);
                            messageType.SenderType = ComponentType.CommunicationServer;
                            messageType.Sender = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
                            var response = messageType.Run(T);

                            log.Info("SERWER ODPOWIADA: ");

                            if (response != null)
                                mesagesList.AddRange(response);
                        }
                    }


                  for(int i=0;i<mesagesList.Count;i++)
                    {
                        var r = mesagesList[i];
                        if (r == null) continue;
                        XmlSerializer serializer = new XmlSerializer(r.GetType());
                        try
                        {
                            serializer.Serialize(stream, r);
                            stream.Flush();
                        }
                        catch (Exception e)
                        {
                            serializer.Serialize(stream, r);
                        }

                        if (mesagesList.Count > 1 && i!=mesagesList.Count-1)
                            stream.Write(new byte[] { 23 }, 0, 1);
                    }

                }
            }

        }
    }

并且你想在模板中迭代它,你最终会做这样的事情:

$scope.items = {
 id1: {
  foo: 'bar'
 }
 id2: {
  foo: 'foo'
 }
}

如果要迭代非关联数组,则不需要在模板上定义元组。