简体   繁体   English

如何使用Angular JS在MVC SPA中实现级联下拉列表?

[英]How to implement Cascading Drop Down List in MVC SPA using Angular JS?

I'm trying to learn MVC Single Page Application(SPA) with angularjs, and i've no idea that how to implement Cascading Drop Down List in it. 我正在尝试使用angularjs学习MVC单页应用程序(SPA),而且我不知道如何在其中实现级联下拉列表。 Any help is appreciated. 任何帮助表示赞赏。

Thanks in advance, 提前致谢,

Naga Phaneendra. 娜迦·费纳德拉(Naga Phaneendra)。

I think that you want to support Ajax in your cascading drop-down list. 我认为您想在级联下拉列表中支持Ajax。 This link to JSFIDDLE contains good example for cascading DDL using both, Ajax and Static list. 这个指向JSFIDDLE的链接包含了一个使用Ajax和Static列表级联DDL的好例子。 http://jsfiddle.net/annavester/Zd6uX/ http://jsfiddle.net/annavester/Zd6uX/

The Html of the Cascading 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>

As you can see, we use ng-options to populate the data in the DDL. 如您所见,我们使用ng-options填充DDL中的数据。 the DDL will return in the $scope the selected object. DDL将在$ scope中返回所选对象。 so don't wary about how to handle id and title that will appears in options of the DDL. 因此,请不要担心如何处理将出现在DDL选项中的ID和标题。

next the controller code as follow: 接下来的控制器代码如下:

function AjaxCtrl($scope) {
$scope.countries = ['usa', 'canada', 'mexico', 'france'];
$scope.$watch('country', function(newVal) {
    if (newVal) $scope.cities = ['Los Angeles', 'San Francisco'];
});
$scope.$watch('city', function(newVal) {
    if (newVal) $scope.suburbs = ['SOMA', 'Richmond', 'Sunset'];
});
}

as you can see, we use $watch to watch change in the DDL. 如您所见,我们使用$ watch监视DDL中的更改。 replace this line of code 替换此行代码

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

with the code that fires Ajax request to select data based on newVal.ID and fill cities with results using $http.get 使用触发Ajax请求以基于newVal.ID选择数据并使用$ http.get向城市填充结果的代码

Most of the time this sort of menu is accomplished by generating some sort of nested html structure (usually <ul> and <li> tags), then applying a stylesheet and some javascript to show or hide the child elements until they're clicked. 通常,通过生成某种嵌套的html结构(通常是<ul><li>标记),然后应用样式表和一些javascript来显示或隐藏子元素,直到单击它们,才能完成这种菜单。 There are a billion examples of this online so I will focus this answer on how to generate the nested html (which is the hard part). 在线上有十亿个示例,因此我将把答案集中在如何生成嵌套的html(这是最困难的部分)上。

You can accomplish this using recursive ng-repeat along with an inline template. 您可以使用递归ng-repeat和内联模板来完成此操作。

First off you need some sort of tree model in your scope. 首先,您需要在您的范围内建立某种树模型。

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

Now in your view, you can do. 现在,您可以执行此操作。

<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>

Here's a link to a working example. 这是一个工作示例的链接。 http://plnkr.co/edit/V6aVx0JeBFwrUgPZs0vw?p=preview http://plnkr.co/edit/V6aVx0JeBFwrUgPZs0vw?p=预览

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM