2016-08-13 34 views
1

我是新的angularJs,我想注入一個字符串列表,我得到它使用restful web服務到jSON列表中。 連接列表如何繼續由getAllConnectedApp返回的對象。注入一個json格式的列表

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; 
      }); 
     }; 

    } 
    ]); 
+1

你是什麼錯誤? –

+0

所有的應用程序無法顯示 – user3237201

+1

什麼意思是「無法顯示」?網頁瀏覽器的控制檯中是否有錯誤? – smefju

回答

1

你在那裏有語法錯誤,你不能在對象字面定義中有循環。

此外,您可移植只想在從服務器獲取響應後才更新菜單。 因爲您的代碼在您執行循環時不合邏輯,因此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', 
          }); 
         } 
        }); 
      }; 

     } 
    ]); 
+0

首先感謝您的回答,然後我希望在加載頁面時顯示連接列表,所以得到服務器的響應必須加載 – user3237201

+0

目前菜單首先會顯示爲ADD ADDECTION項, ,因爲getAllConnectedApp是異步的,您可以使用加載程序並僅在收到響應後才顯示菜單,或者如果您正在使用'ui-router',則使用狀態解析,僅在獲取菜單後才加載狀態。 請參閱https://github.com/angular-ui/ui-router/wiki'resolve'部分。 – BarakD