Angularjs Angular.js确定哪个按钮导致提交的最佳方法是什么?
我在一个简单的登录表单上有两个按钮,它们位于标题栏的下拉列表中,在我的单页应用程序的查看/内容部分之外。表单上有两个按钮: 编辑:两个按钮都需要提交表单,但我有两种不同的结果;一个注册新成员,另一个登录现有成员。我不想在多个部分上处理这个问题 我的网站Angularjs Angular.js确定哪个按钮导致提交的最佳方法是什么?,angularjs,button,directive,Angularjs,Button,Directive,我在一个简单的登录表单上有两个按钮,它们位于标题栏的下拉列表中,在我的单页应用程序的查看/内容部分之外。表单上有两个按钮: 编辑:两个按钮都需要提交表单,但我有两种不同的结果;一个注册新成员,另一个登录现有成员。我不想在多个部分上处理这个问题 我的网站 因此,我的问题只是使用ng submit=“myfunction()”我可以确定按下了哪个按钮吗?我不确定我是否正确理解了您的问题,但您可以基于 为每个ng提交调用不同的函数,例如ng submit=“myFunction1()”和ng s
因此,我的问题只是使用
ng submit=“myfunction()”
我可以确定按下了哪个按钮吗?我不确定我是否正确理解了您的问题,但您可以基于
- 为每个ng提交调用不同的函数,例如
和ng submit=“myFunction1()”
ng submit=“myFunction2()”
- 您还可以使用参数
ng submit=“myFunction(from)”
- 您还可以将特殊的
对象作为参数$event
传递。此对象包含目标信息ng submit=“myFunction($event)”
<form name="loginForm" ng-submit="login()" ng-controller="homeController">
<div class="form-group btn-group-justified">
<div class="btn-group">
<button type="submit" class="btn btn-default" button-id="join">New?Joinus</button>
<input type="hidden" class="btn" />
</div>
<div class="btn-group inline">
<input type="hidden" class="btn" />
<button type="submit" class="btn btn-primary active" button-id="login">Log in</button>
</div>
</div>
</form≥
。。。现在是指令。
接下来,我使用一个指令处理单击任意一个按钮。这样做的目的是允许我识别按钮,并将其传递给$scope
。这实际上是练习的主要目的,但我意识到我现在可以将它绑定到任何我怀疑有点击的地方,并将一些数据传递到示波器
app.directive('buttonId', function() {
return {
restrict: "A",
link: function(scope, element, attributes) {
element.bind("click", function(){
// able to get the name of the button and pass it to the $scope
// this is executed on every click
scope.buttons.chosen = attributes.buttonId;
// alert(attributes.buttonId + scope.buttons.chosen);
});
}
}
});
另一种方法是为这个按钮设置属性dirty,然后检查哪个按钮是dirty 例如,如果您有一个名为“myForm”的表单,您可以编写如下内容:
<form name="myForm" id="myForm" ng-submit="save()" ng-model="myForm" novalidate>
<input type="submit" name="save" ng-model="btnSave" ng-click="(frmForm.save.$setDirty())" />
<input type="submit" name="saveOut" ng-model="btnSaveOut" ng-click="(frmForm.saveOut.$setDirty())" />
</form>
您可以在ng click中获取$event的句柄,获取其目标,然后获取其id,但我不建议您使用这种方式:
<input type="submit" id="test" data-ng-click="showAlert($event)">
Click Me
</button>
$scope.showAlert = function(event){
alert(event.target.id);
}
点击我
$scope.showAlert=函数(事件){
警报(event.target.id);
}
我不明白答案。您只能在
标签上使用一个ng submit
,除非您建议在按钮级别使用它。对不起,我的意思也是在按钮级别,因为提交是在表单级别。我的错。切题地说,这看起来像是令人困惑的UI设计。如果我是你,我会在视觉上把这两个动作分开。我在尝试新的东西。
app.directive('buttonId', function() {
return {
restrict: "A",
link: function(scope, element, attributes) {
element.bind("click", function(){
// able to get the name of the button and pass it to the $scope
// this is executed on every click
scope.buttons.chosen = attributes.buttonId;
// alert(attributes.buttonId + scope.buttons.chosen);
});
}
}
});
<form name="myForm" id="myForm" ng-submit="save()" ng-model="myForm" novalidate>
<input type="submit" name="save" ng-model="btnSave" ng-click="(frmForm.save.$setDirty())" />
<input type="submit" name="saveOut" ng-model="btnSaveOut" ng-click="(frmForm.saveOut.$setDirty())" />
</form>
if ($scope.btnSave.$dirty){
alert("First was clicked)}
else{
alert("First was clicked)}
}
<input type="submit" id="test" data-ng-click="showAlert($event)">
Click Me
</button>
$scope.showAlert = function(event){
alert(event.target.id);
}