将列表注入 json 格式

injecting a list into a json format

我是 angularJs 的新手,我想将使用 restful 网络服务获得的字符串列表注入 jSON 列表。 连接列表如何处理 getAllConnectedApp 返回的对象。

angular
  .module('theme.core.navigation_controller', ['theme.core.services'])
  .controller('NavigationController', ['$scope', '$location', '$timeout', '$filter', '$http', '$cookieStore', '$interval',
    function($scope, $location, $timeout, filter, $http, $cookieStore, $interval) {
      'use strict';
      $scope.filterOptions = {
        filterText: '',
        useExternalFilter: true
      };
      $scope.connections = [];
      $scope.menu = [{
        label: 'HOME',
        iconClasses: 'glyphicon glyphicon-home',
        url: '#/'
      }, {
        label: 'ORACLE MONITORING',
        iconClasses: 'glyphicon glyphicon-unchecked',
        children: [{
          label: 'SESSIONS',
          url: '#/general'

        }, {
          label: 'ADVANCED MONITORING',
          url: '#/advanced-monitoring'
        }, {
          label: 'CONFIGURATION',
          url: '#/configuration'
        }]
      }, {
        label: 'CODE TRACER',
        iconClasses: 'glyphicon glyphicon-check',
        children: [{
              label: 'ADD CONNECTION',
              url: '#/addConnectionApp'

            },
            for (var i = 0; i < $scope.connections.length; i++) {
              {
                label: $scope.connections[i],
                url: '#/codetracer',

              }
            }
          ]
          //url: '#/codetracer'
      }];

      $scope.getAllConnectedApp = function() {
        console.log("GET ALL CONNECTED APPLICATIONS...");
        $http.get("http://localhost:8090/api/personne/allConnection")
          .success(function(connections) {
            console.log(connections);
            $scope.connections = connections;
          });
      };

    }
  ]);

你那里有语法错误,你不能在对象字面量定义中有循环。

此外,您可能希望仅在从服务器返回响应后更新菜单。 因为在执行循环时您的代码不合逻辑,scope.connections 仍然是一个空数组。

像这样:

angular
    .module('theme.core.navigation_controller', ['theme.core.services'])
    .controller('NavigationController', ['$scope', '$location', '$timeout', '$filter', '$http', '$cookieStore', '$interval',
        function ($scope, $location, $timeout, filter, $http, $cookieStore, $interval) {
            'use strict';
            $scope.filterOptions = {
                filterText: '',
                useExternalFilter: true
            };
            $scope.connections = [];
            var connectionsMenu = [{
                label: 'ADD CONNECTION',
                url: '#/addConnectionApp'

            }];

            $scope.menu = [{
                label: 'HOME',
                iconClasses: 'glyphicon glyphicon-home',
                url: '#/'
            }, {
                    label: 'ORACLE MONITORING',
                    iconClasses: 'glyphicon glyphicon-unchecked',
                    children: [{
                        label: 'SESSIONS',
                        url: '#/general'

                    }, {
                            label: 'ADVANCED MONITORING',
                            url: '#/advanced-monitoring'
                        }, {
                            label: 'CONFIGURATION',
                            url: '#/configuration'
                        }]
                }, {
                    label: 'CODE TRACER',
                    iconClasses: 'glyphicon glyphicon-check',
                    children: [connectionsMenu]
                    //url: '#/codetracer'
                }];



            $scope.getAllConnectedApp = function () {
                console.log("GET ALL CONNECTED APPLICATIONS...");
                $http.get("http://localhost:8090/api/personne/allConnection")
                    .success(function (connections) {
                        console.log(connections);
                        $scope.connections = connections;
                        for (var i = 0; i < connections.length; i++) {
                            connectionsMenu.push({
                                label: connections[i],
                                url: '#/codetracer',
                            });
                        }
                    });
            };

        }
    ]);