繁体   English   中英

如何将选择为“文本”的SELECT框发送到按钮上的函数,以便使用angular进行ng-click?

[英]How can I send a SELECT box selected “text” to a function on a button for ng-click using angular?

好的,我有这个HTML页面,其中包含一个select元素。 该select元素将具有三个选择(请参阅下面这些选择的$ scope定义...:

<div id="mainID" ng-controller="theController">
    <form name="assetTypeForm" class="form-horizontal" role="form" novalidate> 
        <select id="assetTypeSelect" name="asset.assetTypeList" style="width: 135px;"
            ng-model="asset.assetTypeList" 
            ng-options="option.name for option in assetTypeListOptions track by option.value"
            ng-change="regularAssetTypeToggleClick(asset)"
            class="form-control" required>
            <!-- This is a HACK to trick Angular to make the first option value = 0 -->
            <option value="" style="display: none;">Choose Type</option>
        </select>
        <button type="button" 
                ng-click="createAsset({{asset.assetTypeList}});" //This is the value I'm trying to pass...
                class="btn btn-primary btn-small"> 
                Create Asset
        </button>
    </form>
</div>

接下来,在我的控制器中,从按钮中调用此函数:

$scope.createAsset = function (assetType) { //As you can see with assetType (arg) I want to pass what the user selected in the dropdown select box.

        $scope.$broadcast('show-errors-check-validity');
        console.log("The asset I want to create is: " + assetType);

        if ($scope.assetTypeForm.$invalid) {

            console.log("BUMMER! The assetTypeForm is NOT valid: " + $scope.assetTypeForm.$invalid);
            $scope.submitted = false;
            return;

        } else {

            //Open the dialog for creating a graphic element
            $scope.openCreateElement(assetType);

        }

    };

我有assetType下拉列表的定义:

    $scope.assetTypeListOptions = [
        {
            name: 'Choose Type...',
            value: 'choose'

        }, {
            name: 'EULA',
            value: 'eula'

        }, {
            name: 'GRAPHIC',
            value: 'graphic'

        }];
    //This sets the default for the list
    $scope.assetTypeList = $scope.assetTypeListOptions[0];

我在console.log中得到的是:

The asset I want to create is: [object Object] <-- Here, is where either EULA, Graphic or Choose Type... where Choose Type... will throw an alert to tell the user, via show-errors{} that they NEED to select either EULA or Graphic.

而已....

谢谢

OK更新:作为回应,评论:

所以,你的意思是 我可以将您建议的内容传递给函数:[[[$ scope.assetTypeListOptions [$ scope.asset.assetTypeList] .value]]]到函数中:“ createAsset({{asset.assetTypeList}});” 像这样?

按命令:

  ng-click="createAsset({{asset.assetTypeList}});"

您无需在指令中使用{{}}

  ng-click="createAsset(asset.assetTypeList);"

但是您甚至不需要通过它,因为您选择的模型始终可以作为

$scope.asset.assetTypeList

另外你有错误的初始化:

$scope.assetTypeList = $scope.assetTypeListOptions[0];

您使用ng-model="asset.assetTypeList" ,所以您应该初始化:

$scope.asset.assetTypeList = $scope.assetTypeListOptions[0];

确保$scope.asset之前$scope.asset初始化。

否则,请使用ng-model="assetTypeList"

您应该能够使用ng-model指令来应用数据绑定。

因此,“ <选择>”成为

 <select ng-model="fieldName" >

然后,如果需要调用的函数在控制器中,则可以使用$ scope.fieldName获取select的值。

暂无
暂无

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

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