gpt4 book ai didi

javascript - 如何将选择 "text"的 SELECT 框发送到按钮上的函数以使用 angular 进行 ng-click?

转载 作者:行者123 更新时间:2023-11-30 17:07:58 24 4
gpt4 key购买 nike

好的,我有这个包含选择元素的 HTML 页面。该选择元素将有三个选择(请参阅下面这些选择的 $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>

接下来在我的 Controller 中,我从按钮调用了这个函数:

$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.

就是这样....

谢谢

确定更新:作为回应评论:

所以,你的意思是;我可以将您在此处建议的内容传递给函数:[[[ $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之前已经初始化。

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

关于javascript - 如何将选择 "text"的 SELECT 框发送到按钮上的函数以使用 angular 进行 ng-click?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27570726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com