如何从angularjs的json文件中获取菜单键

How to get the menu key from the json in angularjs

本文关键字:获取 菜单 文件 json angularjs      更新时间:2023-09-26

我想获得"menu1"或"menu2"字段等,如何在angularjs中做到这一点?json如下:

{
"menu1": [
   {
     "item": "1",
     "Auth": "content/articleList",
   },
   {
     "item": "2",
     "Auth": "content/articleList",
   }],
 "menu2": [
   {
     "item": "3",
     "Auth": "publish/cacheSetting",
   },
   {
     "item": "4",
     "Auth": "publish/juggleList",
   }]
}

你可以这样做:

<div ng-repeat="(key, menu) in menus">
  {{ key }}
  <div ng-repeat="item in menu">
    {{item.item}} : {{item.Auth}}
  </div>
</div>

如果你有一个服务,并且想在控制器中使用它,那么你需要做以下的事情来在服务中加载JSON,并从控制器中调用它。

App.factory("MenuService", [ '$http', function($http) {
    return {
        getMenus: function() {
            return $http.get( '/menuService' );
        }
    }
}]);

App.controller("SomeController", ['$scope', 'MenuService', function($scope,MenuService) {
    $scope.doSomething = function() {
        MenuService.getMenus().success( result ) {
            $scope.menu1 = result.menu1;
            $scope.menu2 = result.menu2;
            // You got menu1 and menu2 now do something
        }
    }
}]);