繁体   English   中英

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

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

我正在尝试使用angularjs学习MVC单页应用程序(SPA),而且我不知道如何在其中实现级联下拉列表。 任何帮助表示赞赏。

提前致谢,

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

我认为您想在级联下拉列表中支持Ajax。 这个指向JSFIDDLE的链接包含了一个使用Ajax和Static列表级联DDL的好例子。 http://jsfiddle.net/annavester/Zd6uX/

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

如您所见,我们使用ng-options填充DDL中的数据。 DDL将在$ scope中返回所选对象。 因此,请不要担心如何处理将出现在DDL选项中的ID和标题。

接下来的控制器代码如下:

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

如您所见,我们使用$ watch监视DDL中的更改。 替换此行代码

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

使用触发Ajax请求以基于newVal.ID选择数据并使用$ http.get向城市填充结果的代码

通常,通过生成某种嵌套的html结构(通常是<ul><li>标记),然后应用样式表和一些javascript来显示或隐藏子元素,直到单击它们,才能完成这种菜单。 在线上有十亿个示例,因此我将把答案集中在如何生成嵌套的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>

这是一个工作示例的链接。 http://plnkr.co/edit/V6aVx0JeBFwrUgPZs0vw?p=预览

暂无
暂无

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

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