javascript - AngularJS 中的下拉菜单

标签 javascript html angularjs node.js http

我的 Angular 应用程序中有两个下拉菜单。当我在第一个下拉菜单中选择一个选项时,我希望该选择影响第二个下拉菜单。示例:第一个菜单将包含状态消息列表。当我选择“需要维护”时,它会将第二个菜单更改为与维护相关的部门。或者如果我选择状态“丢失”,它会将第二个菜单更改为与丢失状态相关的部门。这是我的代码和设置:

.controller('HomeCtrl', function($scope, $rootScope, $http, $ionicPlatform) {
	// This disables the user from being able to go back to the previous view
	$ionicPlatform.registerBackButtonAction(function (event) {
		event.preventDefault();
	}, 100);
	
	// This function only gets called  when the submit button is hit on the messaging screen. It posts to server for sending of message
    $scope.submit = function() {
        	$http.post('http://localhost:8000/', { 
				messageText: $scope.messageText, 
				name: window.localStorage.getItem("username")
			});	
			$scope.messageText = ""; //clearing the message box
		}
})
<div class="form1" ng-controller="HomeCtrl">
              <form class="form">
				<select placeholder="Select a Subject" ng-model="selectSubject" data-ng-options="option.subject for option in subject"></select>
				  
				<select placeholder="Select a Department" ng-model="selectDept" data-ng-options="option.dept for option in dept"></select> 
                  
                <input type="text" class="messageText" placeholder="Message" ng-model="messageText">
                 
                <button class="button" ng-click="submit()"><span>Submit</span></button>
              </form>      
          </div>

那是我的 Controller ,与同时发布的 html 相关。

我知道如何从存储信息的 Node 服务器获取我需要的信息。我需要帮助的是在第一个菜单中单击某个选项时更改第二个菜单中的选项。

我正在考虑在单击一个选项时调用 http.get,然后将填充第二个菜单。第一个菜单将是静态的,不会改变。

最佳答案

我刚刚开始摆弄 Angular 和数据库,并且能够根据另一个选择动态填充一个选择。

下面是我的两个选择框的 HTML(我的第二个选择框是一个multiple,但您显然可以删除该属性):

<label>Select a Table</label>
<select name="tableDropDown" id="tableDropDown" 
  ng-options="table.name for table in data.availableTables"
  ng-model="data.selectedTable"
  ng-change="getTableColumns()">
</select>

<label>Select Columns</label>
<select name="columnMultiple" id="columnMultiple"
  ng-options="column.name for column in data.availableColumns"
  ng-model="data.selectedColumns"
  multiple>
</select>

我有下面的 Controller 。 init 函数清除两个 select 的所有数据源,检索表列表(对于第一个 select),设置将选定的表添加到列表中的第一个表,然后调用第二个函数。

第二个函数(由于所选表更改时也称为 ng-change 指令)都会检索所选表的元数据,并使用它来填充第二个 select 包含可用列的列表。

.controller('SimpleController', function($scope, $http) {

  init();

  function init() {
    $scope.data = {
      availableTables: [],
      availableColumns: [],
      selectedTable: {}
    };

    $http.get("http://some.server.address")
    .then(function (response) {
      $scope.data.availableTables = response.data.value;
      $scope.data.selectedTable = $scope.data.availableTables[0];
      $scope.getTableColumns();
    });
  }

  $scope.getTableColumns = function () {
    $scope.data.selectedColumns = [];
    table = $scope.data.selectedTable.url;
    if (table != "") {
      $http.get("http://some.server.address/" + table + "/$metadata?@json")
      .then(function (response) {
        $scope.data.availableColumns = response.data.value;
      });
    }
  } 

...

});

关于javascript - AngularJS 中的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377039/

相关文章:

javascript - 根据定义的索引检查输入项。 Javascript 或 Jquery

javascript - Angular 列表项处于事件状态(ng-click)

javascript - 如何将 rowGroup 与 DataTables 一起使用,以便 rowGroup 分隔符右对齐?

javascript - 是否有用于单词列表的 "movie credits scrolling"样式插件?

javascript - 将图像 URL 发送到特定文本字段的 Google Chrome 扩展程序

html - 使用 Bootstrap 溢出页面的内容

javascript - 如何从 AngularJs Controller 中的 ION-Range-Slider 获取 "from"和 "to"值

angularjs - 如何定位缺少 $injector 错误?

javascript - 为什么 HighStocks 报价计算不均匀?

javascript - 打开和关闭菜单