如何将选定的“文本”选择框发送到按钮上的函数,以便使用角度进行 ng 单击
How can I send a SELECT box selected "text" to a function on a button for ng-click using angular?
好的,我有一个包含选择元素的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>
接下来在我的控制器中,我从按钮调用了这个函数:
$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);
}
};
我有资产类型的定义下拉列表:
$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];
我在控制台中得到的是.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"
.
您应该能够使用 ng-model 指令来应用数据绑定。
所以"><选择>"变成了选择>
<select ng-model="fieldName" >
然后,如果需要调用的函数位于控制器中,则可以使用 $scope.fieldName 获取 select 的值。
相关文章:
- ng更改函数未被调用,并且没有错误
- angular.js:13424错误:[ng:areq]参数'fn'不是函数,getObject
- AngularJS:如何使用java脚本函数更改跨度的ng-show条件
- 使用jqGrid列格式化程序函数使ng-click工作
- 想要将ng个repeat对象传递给自定义筛选函数
- ng用异步函数模拟怪异行为
- ng-click指令没有调用整个函数
- AngularJS ng repeat显示原型函数未定义
- ng中继器打印结果0;当我运行函数时
- 调用时未输入角度 ng 单击函数
- 在 Angular 指令中定义一个用于 ng-click 的函数
- ng单击触发函数,但无法更改$scope.var
- 在指令之后加载的角度ng-init函数
- 是否可以在 ng-click 中调用函数字符串
- 如何在 ng-click 函数中更改$rootScope值
- 在引导模式中使用“ng click”触发函数
- AngularJS$rootScope函数ng更改
- 如何在角函数ng-repeat中创建新行
- CSS "float: none;"阻止Javascript函数"ng-click&quo
- 调用angularjs内的函数ng重复