javascript 将 ng-model 作为 ng-click 函数参数传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31905622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass ng-model as a ng-click function parameter
提问by Saorikido
Is that possible to pass a ng-model
as a function parameter? for example:
可以将 ang-model
作为函数参数传递吗?例如:
<input type="text" ng-model="myModel">
<button ng-click='save('button1', myModel)'>save</button>
<button ng-click='save('button2', myModel)'>save</button>
<button ng-click='save('button3', myModel)'>save</button>
....
var save = function(buttonIndex, myModel){
myModel = buttonIndex + ' clicked';
}
....
In my case,I have more than one ng-model (6 sets), and I don't want to create 6 sets similar save functions in the controller which only the model is different, if I can pass the model as parameter, I would need to create a single save function is enough.
就我而言,我有不止一个 ng-model(6 组),我不想在控制器中创建 6 组类似的保存功能,只有模型不同,如果我可以将模型作为参数传递,我只需要创建一个保存功能就足够了。
回答by Sajeetharan
Since you are already having your model in $scope you could use it directly, otherwise Create a function in your corresponding controller like this,
由于您已经在 $scope 中拥有您的模型,您可以直接使用它,否则在相应的控制器中创建一个函数,如下所示,
$scope.save = function(model) {
//do whatever you want
}
Example:
例子:
回答by Diego87
This is a easy and fast solution for little operations like your:
对于像您这样的小操作,这是一个简单快速的解决方案:
<input type="text" ng-model="myModel">
<button ng-click="myModel='button1 clicked'">save1</button>
<button ng-click="myModel='button2 clicked'">save2</button>
<button ng-click="myModel='button3 clicked'">save3</button>
it is not passing parameters, but in the on-click definition you can do also normal code.
它不传递参数,但在点击定义中,您也可以执行普通代码。
回答by Chris
I had a similar need. In an ionic form, I have 6 fields of same type/data for which the values could be set by button, and I didn't want to duplicate the function in the controller. I needed to be able to call a function on button click, passing a field (ng-model) name as a parameter, and wasn't working. But finally this works. Took a bit to figure it out since I'm new to angular/ionic. Hope it helps someone because there wasn't a lot of info on this.
我也有类似的需求。在离子形式中,我有 6 个相同类型/数据的字段,可以通过按钮为其设置值,并且我不想在控制器中复制该功能。我需要能够在单击按钮时调用一个函数,将字段(ng-model)名称作为参数传递,但它不起作用。但最终这有效。因为我是角度/离子的新手,所以花了一些时间来弄清楚。希望它对某人有所帮助,因为这方面的信息不多。
First couple fields example:
第一对字段示例:
<div class="item item-input-inset">
<label class="item item-input-wrapper">
<i class="icon ion-edit placeholder-icon"></i>
<input type="text" placeholder="00:00" ng-model="formdata.time1">
</label>
<button class="button" ng-click="settime('time1')">Now</button>
</div>
<div class="item item-input-inset">
<label class="item item-input-wrapper">
<i class="icon ion-edit placeholder-icon"></i>
<input type="text" placeholder="00:00" ng-model="formdata.time2">
</label>
<button class="button" ng-click="settime('time2')">Now</button>
</div>
And in controller:
在控制器中:
.controller('YourCtrl', function ($scope) {
$scope.formdata = {};
$scope.settime = function(field){
var d = new Date();
$scope.formdata[field] = d.getUTCHours()+':'+(d.getUTCMinutes() < 10 ? '0'+d.getUTCMinutes():d.getUTCMinutes());
}
});
If you need to pass a value from a button, you could add parameter:
如果您需要从按钮传递值,您可以添加参数:
<div class="item item-input-inset">
<label class="item item-input-wrapper">
<i class="icon ion-edit placeholder-icon"></i>
<input type="text" ng-model="formdata.yourfield">
</label>
<button class="button" ng-click="yourfunction('This Value','yourfield')">This</button>
<button class="button" ng-click="yourfunction('That Value','yourfield')">That</button>
</div>
And same in controller:
同样在控制器中:
.controller('YourCtrl', function ($scope) {
$scope.formdata = {};
$scope.yourfunction = function(value,field){
$scope.formdata[field] = value;
}
});