Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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
Asp.net mvc 4 如何使用Angular JS在MVC SPA中实现级联下拉列表?_Asp.net Mvc 4_Angularjs_.net 4.0_Single Page Application_Cascadingdropdown - Fatal编程技术网

Asp.net mvc 4 如何使用Angular JS在MVC SPA中实现级联下拉列表?

Asp.net mvc 4 如何使用Angular JS在MVC SPA中实现级联下拉列表?,asp.net-mvc-4,angularjs,.net-4.0,single-page-application,cascadingdropdown,Asp.net Mvc 4,Angularjs,.net 4.0,Single Page Application,Cascadingdropdown,我试图学习使用angularjs的MVC单页应用程序(SPA),但我不知道如何在其中实现级联下拉列表。感谢您的帮助 提前感谢, Naga Phaneendra.大多数情况下,这种菜单是通过生成某种嵌套的html结构(通常是和标记)来完成的,然后应用样式表和一些javascript来显示或隐藏子元素,直到单击它们为止。网上有10亿个这样的例子,所以我将把这个答案集中在如何生成嵌套html(这是最难的部分)上 您可以使用递归ng repeat和内联模板来完成此操作 首先,在您的范围内需要某种树模型

我试图学习使用angularjs的MVC单页应用程序(SPA),但我不知道如何在其中实现级联下拉列表。感谢您的帮助

提前感谢,


Naga Phaneendra.

大多数情况下,这种菜单是通过生成某种嵌套的html结构(通常是
  • 标记)来完成的,然后应用样式表和一些javascript来显示或隐藏子元素,直到单击它们为止。网上有10亿个这样的例子,所以我将把这个答案集中在如何生成嵌套html(这是最难的部分)上

    您可以使用递归
    ng repeat
    和内联模板来完成此操作

    首先,在您的范围内需要某种树模型

    var app = angular.module('menuApp', []);
    
    app.controller('MenuController', function($scope) {
        $scope.menu = [
          {
            label : "Main Menu",
            url: "#/main",
            children: [
              { label: "First Child", url: "#/main/1" },
              { label: "Second Child", url: "#/main/2",
                children: [
                  { label: "First Grandchild", url: "#/main/2/1" },
                  { label: "Second Grandchild", url: "#/main/2/2" }
                ]
              },
              { label: "Third Child", url: "#/main/3",
                children: [
                  { label: "Third Grandchild", url: "#/main/3/3" },
                  { label: "Fourth Grandchild", url: "#/main/third/fourth",
                    children: [
                      { label: "First Great-Grandchild", url: "#/main/3/4/1" },
                      { label: "Second Great-Grandchild", url: "#/main/3/4/2" }
                    ]
                  }
                ]
              }
            ]
          }
        ];
    });
    
    现在在你看来,你可以做到了

    <ul ng-controller="MenuController">
      <li ng-repeat="item in menu" ng-include="'menu-tree.html'"></li>
    </ul>
    
    <script type="text/ng-template"  id="menu-tree.html">
      <a href="{{item.url}}">{{item.label}}</a>
      <ul>
        <li ng-repeat="item in item.children" ng-include="'menu-tree.html'"></li>
      </ul>
    </script>
    

    这里有一个链接指向一个工作示例

    大多数情况下,这种菜单是通过生成某种嵌套的html结构(通常是
  • 标记)来完成的,然后应用样式表和一些javascript来显示或隐藏子元素,直到单击它们。网上有10亿个这样的例子,所以我将把这个答案集中在如何生成嵌套html(这是最难的部分)上

    您可以使用递归
    ng repeat
    和内联模板来完成此操作

    首先,在您的范围内需要某种树模型

    var app = angular.module('menuApp', []);
    
    app.controller('MenuController', function($scope) {
        $scope.menu = [
          {
            label : "Main Menu",
            url: "#/main",
            children: [
              { label: "First Child", url: "#/main/1" },
              { label: "Second Child", url: "#/main/2",
                children: [
                  { label: "First Grandchild", url: "#/main/2/1" },
                  { label: "Second Grandchild", url: "#/main/2/2" }
                ]
              },
              { label: "Third Child", url: "#/main/3",
                children: [
                  { label: "Third Grandchild", url: "#/main/3/3" },
                  { label: "Fourth Grandchild", url: "#/main/third/fourth",
                    children: [
                      { label: "First Great-Grandchild", url: "#/main/3/4/1" },
                      { label: "Second Great-Grandchild", url: "#/main/3/4/2" }
                    ]
                  }
                ]
              }
            ]
          }
        ];
    });
    
    现在在你看来,你可以做到了

    <ul ng-controller="MenuController">
      <li ng-repeat="item in menu" ng-include="'menu-tree.html'"></li>
    </ul>
    
    <script type="text/ng-template"  id="menu-tree.html">
      <a href="{{item.url}}">{{item.label}}</a>
      <ul>
        <li ng-repeat="item in item.children" ng-include="'menu-tree.html'"></li>
      </ul>
    </script>
    

    这里有一个链接指向一个工作示例

    我认为您希望在级联下拉列表中支持Ajax。 这个到JSFIDLE的链接包含使用Ajax和静态列表级联DDL的好例子。

    级联DDL的Html:

    <div ng-controller="AjaxCtrl">
      <h1>AJAX - Oriented</h1>
    <div>
        Country: 
        <select id="country" ng-model="country" ng-options="country for country in countries">
          <option value=''>Select</option>
        </select>
    </div>
    <div>
        City: <select id="city" ng-disabled="!cities" ng-model="city" ng-options="city for city in cities"><option value=''>Select</option></select>
    </div>
    <div>
        Suburb: <select id="suburb" ng-disabled="!suburbs" ng-model="suburb" ng-options="suburb for suburb in suburbs"><option value=''>Select</option></select>        
    </div>
    
    如您所见,我们使用$watch来观察DDL中的变化。 替换这行代码

    if (newVal) $scope.cities = ['Los Angeles', 'San Francisco'];
    

    使用激发Ajax请求以基于newVal.ID选择数据并使用$http.get将结果填充到城市的代码,我认为您希望在级联下拉列表中支持Ajax。 这个到JSFIDLE的链接包含使用Ajax和静态列表级联DDL的好例子。

    级联DDL的Html:

    <div ng-controller="AjaxCtrl">
      <h1>AJAX - Oriented</h1>
    <div>
        Country: 
        <select id="country" ng-model="country" ng-options="country for country in countries">
          <option value=''>Select</option>
        </select>
    </div>
    <div>
        City: <select id="city" ng-disabled="!cities" ng-model="city" ng-options="city for city in cities"><option value=''>Select</option></select>
    </div>
    <div>
        Suburb: <select id="suburb" ng-disabled="!suburbs" ng-model="suburb" ng-options="suburb for suburb in suburbs"><option value=''>Select</option></select>        
    </div>
    
    如您所见,我们使用$watch来观察DDL中的变化。 替换这行代码

    if (newVal) $scope.cities = ['Los Angeles', 'San Francisco'];
    
    使用激发Ajax请求的代码,根据newVal.ID选择数据,并使用$http.get使用结果填充城市