简体   繁体   English

将数据从 HTML 传输到外部 Javascript 代码

[英]Transfering data from HTML to external Javascript code

I have a dropdown in HTML code which I am populating using angular.我在 HTML 代码中有一个下拉列表,我正在使用 angular 填充它。 I want the selected value of that dropdown and send it to angular so that angular fetch the data from database and populate in same HTML page.我想要该下拉列表的选定值并将其发送到 angular,以便 angular 从数据库中获取数据并填充到同一 HTML 页面中。 Below is my code.下面是我的代码。

index.html索引.html

<div ng-app="myApp" ng-controller="sourceController" >
    <select class = "mdb-select md-form "  aria-labelledby="dropdownMenuButton" id="sourcesByName">
        <option class ="dropdown-item"  ng-repeat="source in showsource">{{source}}  </option>
    </select>

</div>

index.js索引.js

var Report = angular.module('Report', []);
Report.controller('mainController', function($scope, $http) {
    $scope.showdata = {}

// I want data here

// Get home page
 $http.get('/api/v1/table')
    .success(function(data) {
         $scope.showdata = data;
         console.log(data);
     })
     .error(function(error) {
         console.log('Error: ' + error);
     });
});


// Get all sources
Report.controller('sourceController', function($scope, $http) {
$scope.showsource =[];
$http.get('/api/v1/sources')
   .success(function(data) {
      var l = data.length;
      data1=['Sources'];
      var i;
      for(i=0;i<l;i++){
         data1.push(data[i]["source"]);
      }
      $scope.showsource = data1;
    })
    .error(function(error) {
    console.log('Error: ' + error);
  });
});

there is an table in my html page which I want to populate according to the dropdown value.我的 html 页面中有一个表格,我想根据下拉值填充它。

Thanks in Advance提前致谢

| | suggest you add an ng-model attribute to your select menu eg建议您在选择菜单中添加 ng-model 属性,例如

ng-model="selectedValue"

This will hold the value that is selected and you will be able to access this variable in your controller with $scope.selectedValue You should also add an ng-change attribute to your select menu so that you can call a function whenever an option is selected.这将保存选定的值,您将能够在控制器中使用$scope.selectedValue访问此变量您还应该向选择菜单添加一个ng-change属性,以便您可以在选择选项时调用函数.

HTML select menu code: HTML 选择菜单代码:

 <select class="mdb-select md-form" ng-model="selectedValue" ng-change="selectSource()" aria-labelledby="dropdownMenuButton" id="sourcesByName">
    <option class="dropdown-item"  ng-repeat="source in showsource">{{source}}  </option>
 </select>

In your mainController在你的主控制器中

var Report = angular.module('Report', []);
Report.controller('mainController', function($scope, $http) {
  $scope.showdata = {};

  $scope.selectSource = function(){
    //This function will be called whenever a new option is selected.
    //log the selectedValue to check it
    console.log($scope.selectedValue);
    //perform http request here with the selectedValue in order to retrieve
    //the corresponding data from the database.
    //Once the data is retrieved, we update the $scope.showdata variable. The view will be automatically updated.
  };

});

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

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