【问题标题】:How to get the selected drop down list value on button click without passing the parameters in angularjs如何在按钮单击时获取选定的下拉列表值而不在angularjs中传递参数
【发布时间】:2015-02-10 13:54:49
【问题描述】:

我有一个下拉列表。单击按钮时我会调用一个函数。在此我需要我选择的所有值如何做到这一点

以下是我的代码

HTML

<html ng-app="myapp">
<head>
<script src="angular/angular.js"></script>
</head>
<body>
<div ng-app ng-controller="MyCtrl">
    <select class="form-control" ng-model="firstType" ng-change="onRegionChange(firstType)" ng-options="item.id as item.name for item in items">
    </select>
    <input type="submit" ng-click='resultView()'/>
</div>
</body>
</html>

脚本

<script>
var myapp = angular.module('myapp', []);

myapp.controller('MyCtrl', function ($scope,$http) 
{
    $scope.Data = {};
    $scope.items = [{id:1, name:'First'}, {id:2, name:'Second'}, {id:3, name:'Third'}];

    $scope.onRegionChange = function(firstType)
    {
       console.log(firstType);
    }
    $scope.resultView = function() 
    {
        console.log(firstType);
    }
});

</script>

非常感谢任何建议或帮助。

单击按钮时,控制台中显示错误

谢谢 这是错误截图

【问题讨论】:

  • 请出示您的items
  • 在改变它的工作,但在按钮上点击它的显示 1
  • 试试ng-click="resultView()"而不是ng-click='resultView()'
  • 两者都知道,但我试过了它不起作用

标签: php html angularjs


【解决方案1】:

你可以给resultView函数添加一个参数。像这样:

<input type="submit" ng-click='resultView(firstType)'/>

并在这里添加。

$scope.resultView = function(firstType) 
    {
        console.log(firstType);
    }

【讨论】:

  • 我在问题中指定我需要一个不作为参数传递的解决方案,因为我有数千个值要传递
【解决方案2】:
$scope.resultView = function(firstType) 
    {
        console.log(this.firstType);
    }

只是你必须添加“this.firstType”然后它工作了漫长的一天后我发现了这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2023-03-15
    • 2012-12-13
    相关资源
    最近更新 更多